214

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 -

  1. Why is this happening?
  2. How can I do set operations (especially set difference) without losing the initial order?
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
d.putto
  • 7,185
  • 11
  • 39
  • 45
  • 182
    @KarlKnechtel - Yes "order is a meaningless concept for sets...in mathematics" but I have real world problems :) – d.putto Mar 21 '12 at 11:32
  • 4
    On CPython 3.6+ `unique = list(dict.fromkeys([1, 2, 1]).keys())`. This works because `dict`s preserve insertion order now. – Boris Verkhovskiy May 06 '20 at 22:27

16 Answers16

194
  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)])
    
Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 6
    None object costs 16 bytes. If only there is an default OrderedSet(). :( – Sean Nov 06 '17 at 08:43
  • 6
    @Sean no, they do not. `None` is a language guaranteed singleton. In CPython, a the actual cost is just the pointer (although that cost is always there, but for a dict, you can almost consider `None` and other singletons or shared references "free"), so a machine word, likely 8 bytes on modern computers. But yeah, it is not as space efficient as a set could be. – juanpa.arrivillaga Aug 14 '19 at 20:41
  • 4
    On CPython 3.6+ you can just do `dict.fromkeys([1, 2, 1]).keys()` because regular `dict`s preserve order too. – Boris Verkhovskiy May 06 '20 at 22:27
  • @Boris This has only been part of the language specification starting from Python 3.7. While the CPython implementation already preserves insertion order in version 3.6, this is [considered an implementation detail](https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-compactdict) which may not be followed by other Python implementations. – Sven Marnach May 07 '20 at 09:16
  • 2
    @Sven I said CPython. I post this everywhere, I'm just getting tired of writing "CPython 3.6 or any other implementation starting with Python 3.7". It doesn't even matter, everyone is using CPython – Boris Verkhovskiy May 07 '20 at 12:06
80

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]
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • 8
    Two notes regarding order preservation: only as of Python 3.6, and even there, it's considered an implementation detail, so don't rely on it. Apart from that, your code is very inefficient because every time `x.index` is called, a linear search is performed. If you're fine with quadratic complexity, there is no reason to use a `set` in the first place. – Thijs van Dien Dec 29 '16 at 11:56
  • 42
    @ThijsvanDien This is wrong, `set()` is not ordered in Python 3.6, not even as an implementation detail, you're thinking of `dict`s – Chris_Rands Aug 09 '17 at 12:06
  • @Chris_Rands I stand corrected; they seem to be sorted, rather than keeping the insertion order. Either way: implementation detail. – Thijs van Dien Aug 09 '17 at 17:47
  • 9
    @ThijsvanDien No they're not sorted, although sometimes appear so because `int`s often hash to themselves https://stackoverflow.com/questions/45581901/are-sets-ordered-like-dicts-in-python3-6/ – Chris_Rands Aug 09 '17 at 18:26
  • 5
    Try `x=[1,2,-1,20,6,210]` and make it a set. You'll see it's not ordered at all, tested in Python 3.6. – GabrielChu Jul 08 '18 at 01:50
  • @ThijsvanDien it might be that the user wants all the unique values sorted from a list. Using a set in that way will accomplish that goal – zwep Oct 03 '19 at 06:14
  • 15
    I cannot understand why this answer has so many upvotes, it does not keep insertion order, neither returns a set. – Igor Rodriguez Oct 25 '19 at 12:46
  • yes, it'll not maintain the insertion order, so not the correct response. – justjais Jan 24 '20 at 06:31
  • 4
    this is an incredibly inefficient O(n^2) time complexity. – Boris Verkhovskiy May 06 '20 at 15:04
  • 1
    @Boris. Fine. `(lambda l: sorted(set(l), key={v:i for i, v in enumerate(l)}.__getitem__))([1,2,20,6,210])`. Should be `O(n)`, plus whatever `sorted()` is. Happy now? I mean, it still won't work with any duplicate elements, but uhhh meh. – Will Chen Mar 16 '21 at 17:41
  • 8
    So you execute one line of code to end up with an output same as the input you started with... Why this has 70+ upvotes? – Tomerikoo Aug 31 '21 at 18:21
  • this doesn't work! – Jatin Chauhan Nov 28 '21 at 18:59
  • I don't understand how this is still marked as correct. Even the commented out line `set() now should keep the order` is not even true? – thethiny Feb 10 '23 at 01:33
47

Remove duplicates and preserve order by below function

def unique(sequence):
    seen = set()
    return [x for x in sequence if not (x in seen or seen.add(x))]

How to remove duplicates from a list while preserving order in Python

SKB
  • 771
  • 7
  • 14
  • 2
    Exactly what I was using set for, and this solves a main issue with using set for removing duplicates from a list; losing the original list order. – Charles Naccio Aug 11 '21 at 04:45
  • 1
    fantastic solution – Martin Bucher Nov 05 '21 at 12:51
  • I made an edit to `return [x for x in sequence if not (tuple(x) in seen or seen.add(tuple(x)))]` in my case I needed a list of lists to be unique. For example, this supports, `unique([[1, 2, 3], [1, 3, 2], [1, 2, 3]])` where the original doesn't – dnk8n Jan 27 '22 at 10:13
  • This is a fantastic answer and a new use of list comprehension that I never though of before. Anyway, it's important to keep in mind that this returns a list and not a set so you will lost the fast indexing resolution. dict.keys() is still my preferred go-to. – thethiny Feb 10 '23 at 01:36
30

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
lvella
  • 12,754
  • 11
  • 54
  • 106
22

In mathematics, there are sets and ordered sets (osets).

  • set: an unordered container of unique elements (Implemented)
  • oset: an ordered container of unique elements (NotImplemented)

In Python, only sets are directly implemented. We can emulate osets with regular dict keys (3.7+).

Given

a = [1, 2, 20, 6, 210, 2, 1]
b = {2, 6}

Code

oset = dict.fromkeys(a).keys()
# dict_keys([1, 2, 20, 6, 210])

Demo

Replicates are removed, insertion-order is preserved.

list(oset)
# [1, 2, 20, 6, 210]

Set-like operations on dict keys.

oset - b
# {1, 20, 210}

oset | b
# {1, 2, 5, 6, 20, 210}

oset & b
# {2, 6}

oset ^ b
# {1, 5, 20, 210}

Details

Note: an unordered structure does not preclude ordered elements. Rather, maintained order is not guaranteed. Example:

assert {1, 2, 3} == {2, 3, 1}                    # sets (order is ignored)

assert [1, 2, 3] != [2, 3, 1]                    # lists (order is guaranteed)

One may be pleased to discover that a list and multiset (mset) are two more fascinating, mathematical data structures:

  • list: an ordered container of elements that permits replicates (Implemented)
  • mset: an unordered container of elements that permits replicates (NotImplemented)*

Summary

Container | Ordered | Unique | Implemented
----------|---------|--------|------------
set       |    n    |    y   |     y
oset      |    y    |    y   |     n
list      |    y    |    n   |     y
mset      |    n    |    n   |     n*  

*A multiset can be indirectly emulated with collections.Counter(), a dict-like mapping of multiplicities (counts).

pylang
  • 40,867
  • 14
  • 129
  • 121
  • also, there are partial ordered sets (posets) – L F Sep 07 '20 at 19:21
  • 1
    And cosets, but I did not think they were germane to topic of common data structures found in the Python standard library :) – pylang Sep 07 '20 at 21:26
  • Very concisely explained. I was looking to obtain the difference of two sets and maintain the order of the remaining elements. This didn't quite work using dict.fromkeys, but it did work using OrderedDict from collections. This was using python 3.11.2. – Rexovas Apr 20 '23 at 04:30
  • Nevermind... it's still not maintaining the original order after subtracting elements from the other set. Maybe I'm misunderstanding how it's supposed to work. – Rexovas Apr 20 '23 at 04:51
  • @Rexovas This technique emulates properties of an oset thru the feature of the modern dict, i.e. unique, (insertion-)order elements, but in the end dict-keys are still set-like. Thus, the set operations revert to behaving like sets (unordered). – pylang Apr 20 '23 at 07:43
  • @pylang Makes sense, I was able to use the list comprehension solution mentioned by kindall here https://stackoverflow.com/questions/10005367/retaining-order-while-using-pythons-set-difference – Rexovas Apr 21 '23 at 00:30
14

You can remove the duplicated values and keep the list order of insertion with one line of code, Python 3.8.2

mylist = ['b', 'b', 'a', 'd', 'd', 'c']


results = list({value:"" for value in mylist})

print(results)

>>> ['b', 'a', 'd', 'c']

results = list(dict.fromkeys(mylist))

print(results)

>>> ['b', 'a', 'd', 'c']
Alex Ricciardi
  • 181
  • 1
  • 9
  • 3
    This is the best one liner solution – SavindraSingh May 21 '21 at 09:57
  • For larger lists, this would be better off using `None` than an empty `str`. ... `>>> None.__sizeof__() 16 >>> "".__sizeof__() 49` . – ingyhere Sep 23 '21 at 02:23
  • How? It converts the list to a dict and back to a list again in one step. Also, this works in Python 3.7+ since insertion order is now guaranteed. For large data sets using a dict exclusively would be beneficial to prevent large and multiple data structures in memory. – ingyhere Sep 23 '21 at 02:26
8

As denoted in other answers, sets are data structures (and mathematical concepts) that do not preserve the element order -

However, by using a combination of sets and dictionaries, it is possible that you can achieve wathever you want - try using these snippets:

# save the element order in a dict:
x_dict = dict(x,y for y, x in enumerate(my_list) )
x_set = set(my_list)
#perform desired set operations
...
#retrieve ordered list from the set:
new_list = [None] * len(new_set)
for element in new_set:
   new_list[x_dict[element]] = element
jsbueno
  • 99,910
  • 10
  • 151
  • 209
4

Building on Sven's answer, I found using collections.OrderedDict like so helped me accomplish what you want plus allow me to add more items to the dict:

import collections

x=[1,2,20,6,210]
z=collections.OrderedDict.fromkeys(x)
z
OrderedDict([(1, None), (2, None), (20, None), (6, None), (210, None)])

If you want to add items but still treat it like a set you can just do:

z['nextitem']=None

And you can perform an operation like z.keys() on the dict and get the set:

list(z.keys())
[1, 2, 20, 6, 210]
jimh
  • 1,651
  • 2
  • 15
  • 28
2

One more simpler way can be two create a empty list ,let's say "unique_list" for adding the unique elements from the original list, for example:

unique_list=[]

for i in original_list:
    if i not in unique_list:
        unique_list.append(i)
    else:
        pass

This will give you all the unique elements as well as maintain the order.

1

Late to answer but you can use Pandas, pd.Series to convert list while preserving the order:

import pandas as pd
x = pd.Series([1, 2, 20, 6, 210, 2, 1])
print(pd.unique(x))

Output: array([ 1, 2, 20, 6, 210])

Works for a list of strings

x = pd.Series(['c', 'k', 'q', 'n', 'p','c', 'n'])
print(pd.unique(x))

Output ['c' 'k' 'q' 'n' 'p']

Trees
  • 1,245
  • 10
  • 20
0

An implementation of the highest score concept above that brings it back to a list:

def SetOfListInOrder(incominglist):
    from collections import OrderedDict
    outtemp = OrderedDict()
    for item in incominglist:
        outtemp[item] = None
    return(list(outtemp))

Tested (briefly) on Python 3.6 and Python 2.7.

0

In case you have a small number of elements in your two initial lists on which you want to do set difference operation, instead of using collections.OrderedDict which complicates the implementation and makes it less readable, you can use:

# initial lists on which you want to do set difference
>>> nums = [1,2,2,3,3,4,4,5]
>>> evens = [2,4,4,6]
>>> evens_set = set(evens)
>>> result = []
>>> for n in nums:
...   if not n in evens_set and not n in result:
...     result.append(n)
... 
>>> result
[1, 3, 5]

Its time complexity is not that good but it is neat and easy to read.

Ultrablendz
  • 611
  • 7
  • 12
0

It's interesting that people always use 'real world problem' to make joke on the definition in theoretical science.

If set has order, you first need to figure out the following problems. If your list has duplicate elements, what should the order be when you turn it into a set? What is the order if we union two sets? What is the order if we intersect two sets with different order on the same elements?

Plus, set is much faster in searching for a particular key which is very good in sets operation (and that's why you need a set, but not list).

If you really care about the index, just keep it as a list. If you still want to do set operation on the elements in many lists, the simplest way is creating a dictionary for each list with the same keys in the set along with a value of list containing all the index of the key in the original list.

def indx_dic(l):
    dic = {}
    for i in range(len(l)):
        if l[i] in dic:
            dic.get(l[i]).append(i)
        else:
            dic[l[i]] = [i]
    return(dic)

a = [1,2,3,4,5,1,3,2]
set_a  = set(a)
dic_a = indx_dic(a)

print(dic_a)
# {1: [0, 5], 2: [1, 7], 3: [2, 6], 4: [3], 5: [4]}
print(set_a)
# {1, 2, 3, 4, 5}
0

We can use collections.Counter for this:

# tested on python 3.7
>>> from collections import Counter
>>> lst = ["1", "2", "20", "6", "210"]

>>> for i in Counter(lst):
>>>     print(i, end=" ")
1 2 20 6 210 

>>> for i in set(lst):
>>>     print(i, end=" ")
20 6 2 1 210
silver
  • 11
  • 3
  • 1
    Why bother with a `Counter` if you can just do `dict.from_keys()`? The only difference is that the values will be `1` instead of `None`, but the values are not interesting anyway as the point is to emulate a set... – Tomerikoo Aug 31 '21 at 18:29
0

You can remove the duplicated values and keep the list order of insertion, if you want

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

for num in lst :
    if num not in new_lst :
        new_lst.append(num)

# new_lst = [1,2,3]

don't use 'sets' for removing duplicate if 'order' is something you want,

use sets for searching i.e.
x in list
takes O(n) time
where
x in set
takes O(1) time *most cases

-5

Here's an easy way to do it:

x=[1,2,20,6,210]
print sorted(set(x))
The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27