+ 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)
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())...
+ 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...
+ 2
excellent
+ 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))`
+ 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
+ 1
Ashleigh Fielders
yes i want to remove duplicate elements of a list
+ 1
nope. order is not important for this problem
+ 1
thnx Hrvoje
+ 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
+ 1
thnx for this Seb TheS
+ 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.
0
tysm Ashleigh Fielders
i got this
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
0
Hrvoje Are you sure dictionaries are ordered? I would disagree.