2 odpowiedzi
+ 5
It can be also done with a simple for loop or with a comprehension:
res = []
[res.append(i) for i in mylist if i not in res]
print(res)
#output: ['blue', 'green', 'red', 'pink']
But the solution with dict should be preferred. This the case from python 3.7+. With older versions it does not work this way.
+ 1
OK. Just found a way to emulate that using a dict, as it seems Python3 doesn't have ordered sets:
mylist =
['blue', 'blue', 'blue', 'green', 'green', 'red', 'red', 'pink']
mylist = list(dict.fromkeys(mylist).keys())
# This outputs:
['blue', 'green', 'red', 'pink']