Recently I noticed that when I am converting a list
to set
the order of elements is changed and is sorted by character.
Consider this example:
x=[1,2,20,6,210]
print(x)
# [1, 2, 20, 6, 210] # the order is same as initial order
set(x)
# set([1, 2, 20, 210, 6]) # in the set(x) output order is sorted
My questions are –
- Why is this happening?
- How can I do set operations (especially set difference) without losing the initial order?