Converting a list to a set changes element order Converting a list to a set changes element order python python

Converting a list to a set changes element order


  1. A set is an unordered data structure, so it does not preserve the insertion order.

  2. 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:

    >>> a = [1, 2, 20, 6, 210]>>> b = set([6, 20, 1])>>> [x for x in a if x not in b][2, 210]

    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:

    >>> a = dict.fromkeys([1, 2, 20, 6, 210])>>> b = dict.fromkeys([6, 20, 1])>>> dict.fromkeys(x for x in a if x not in b){2: None, 210: None}

    b doesn't really need to be ordered here – you could use a set as well. Note that a.keys() - b.keys() returns the set difference as a set, so it won't preserve the insertion order.

    In older versions of Python, you can use collections.OrderedDict instead:

    >>> a = collections.OrderedDict.fromkeys([1, 2, 20, 6, 210])>>> b = collections.OrderedDict.fromkeys([6, 20, 1])>>> collections.OrderedDict.fromkeys(x for x in a if x not in b)OrderedDict([(2, None), (210, None)])


In Python 3.6, set() now should keep the order, but there is another solution for Python 2 and 3:

>>> x = [1, 2, 20, 6, 210]>>> sorted(set(x), key=x.index)[1, 2, 20, 6, 210]


Answering your first question, a set is a data structure optimized for set operations. Like a mathematical set, it does not enforce or maintain any particular order of the elements. The abstract concept of a set does not enforce order, so the implementation is not required to. When you create a set from a list, Python has the liberty to change the order of the elements for the needs of the internal implementation it uses for a set, which is able to perform set operations efficiently.