+ 2

Given(not input):- [1, 2, 3, 4, 2, 3] ... Output:- [1, 2, 3, 4]

Help! I m struck with a problem. and this problem is a subset of that problem. (language is python)

25th Feb 2020, 10:12 PM
Suraj Das
Suraj Das - avatar
14 Antworten
+ 3
i dont really inderstand your question, can you clarify it please? if you’re trying to remove duplicate elements then look into list(set())...
25th Feb 2020, 10:22 PM
Ashleigh Fielders
Ashleigh Fielders - avatar
+ 3
Ok, well use list(set(x)) where x is the list you want to remove the duplicate elements from. You may/probably will loose the order of items in the resulting list, so if order is important, you will have to figure out a way of overcoming this...
25th Feb 2020, 10:43 PM
Ashleigh Fielders
Ashleigh Fielders - avatar
+ 2
excellent
25th Feb 2020, 10:46 PM
Ashleigh Fielders
Ashleigh Fielders - avatar
+ 2
'set' does the trick, but as Ashleigh wrote, sets are unordered... sets. If you want to keep the order of the list elements you can convert the list to dictionary, which is ordered. Then, same as with set, just convert it back to list: `output = list(dict.fromkeys(input))`
25th Feb 2020, 11:30 PM
Hrvoje
Hrvoje - avatar
+ 2
Set contain a property which is known as no duplicacy soo your input is 1,2,3,4,1,3 where 1 and 3 are repeated so set remove the duplicate elements in set so thats why your output is 1,2,3,4
26th Feb 2020, 7:13 PM
Ujjwal Deval
Ujjwal Deval - avatar
+ 1
Ashleigh Fielders yes i want to remove duplicate elements of a list
25th Feb 2020, 10:28 PM
Suraj Das
Suraj Das - avatar
+ 1
nope. order is not important for this problem
25th Feb 2020, 10:45 PM
Suraj Das
Suraj Das - avatar
+ 1
thnx Hrvoje
26th Feb 2020, 7:13 AM
Suraj Das
Suraj Das - avatar
+ 1
Without sets you could start iterating: i = 0, 1, 2, ... len(list) - 1 In each iteration you could iterate: j = len(list) - 1, len(list) - 2, len(list) - 3, ... i + 1 If list[i] == list[j]: list.pop(j) You should not use range, because the list size changes. Here's a working function which uses the idea: https://code.sololearn.com/ci3tu6byrEa1/?ref=app
27th Feb 2020, 3:59 PM
Seb TheS
Seb TheS - avatar
+ 1
thnx for this Seb TheS
27th Feb 2020, 4:03 PM
Suraj Das
Suraj Das - avatar
+ 1
Seb TheS , I would say that they are, by default. But I checked it now and it seems that before version 3.6 they weren't.
27th Feb 2020, 10:57 PM
Hrvoje
Hrvoje - avatar
0
tysm Ashleigh Fielders i got this
25th Feb 2020, 10:31 PM
Suraj Das
Suraj Das - avatar
0
You're program has 1,2,3 and 4 given is adding nother 2 and 3 plzz remove the last comma and try it again
26th Feb 2020, 10:52 AM
Java
Java - avatar
0
Hrvoje Are you sure dictionaries are ordered? I would disagree.
27th Feb 2020, 3:52 PM
Seb TheS
Seb TheS - avatar