1

I have a list:

lst=[1,1,2,2,3,3,4,4]

After this code:

lst = list(set(lst))

print(lst)

I get this output:

[1, 2, 3, 4]

Also, I have another list:

lst=[6,6,7,7,8,8,9,9]

After this code:

lst = list(set(lst))

print(lst)

I get this output

[8, 9, 6, 7]

This output is not in the original order of the list ([6,7,8,9]). But I expected to get this order.

Also, I have a another list:

lst=[6,6,7,7,8,8,9,9,1,1]

After this code:

lst = list(set(lst))

print(lst)

I get this output:

[1, 6, 7, 8, 9]

But I want the output as [6, 7, 8, 9, 1].

So my question is: how can I take the set of a list while retaining the original order order of list? Thanks in advance.

iz_
  • 15,923
  • 3
  • 25
  • 40
user1999109
  • 421
  • 7
  • 19
  • Yes. It is already answered. But I couldn't find this answer while searching with my search queries. Sorry for the inconvenience.. Now I get the answer by using " list(OrderedDict.fromkeys(items)) " code.. Thank you very much.. – user1999109 Feb 13 '19 at 05:01

0 Answers0