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?
A
setis an unordered data structure, so it does not preserve the insertion order.This depends on your requirements. If you have an normal list, and want to remove some set of elements while preserving the order of the list, you can do this with a list comprehension:
If you need a data structure that supports both fast membership tests and preservation of insertion order, you can use the keys of a Python dictionary, which starting from Python 3.7 is guaranteed to preserve the insertion order:
bdoesn’t really need to be ordered here – you could use asetas well. Note thata.keys() - b.keys()returns the set difference as aset, so it won’t preserve the insertion order.In older versions of Python, you can use
collections.OrderedDictinstead: