0

Can't make a set from a list with repeated items and keep the items order.Does anyone know how to make an ordered set in Python?

Ordered Set in Python

3rd Jun 2020, 3:03 PM
ıllıllı[ M̤e̤V̤e̤R̤s̤S̤ ]ıllıllı
ıllıllı[ M̤e̤V̤e̤R̤s̤S̤ ]ıllıllı - avatar
2 Respuestas
+ 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.
3rd Jun 2020, 3:59 PM
Lothar
Lothar - avatar
+ 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']
3rd Jun 2020, 3:21 PM
ıllıllı[ M̤e̤V̤e̤R̤s̤S̤ ]ıllıllı
ıllıllı[ M̤e̤V̤e̤R̤s̤S̤ ]ıllıllı - avatar