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].
There are no built-in functionalities as an ordered set, but many packages can help you with OrderedSet. As for Python 3.7 and CPython 3.6, regular dict is confirmed to preserve order and perform better than OrderedDict.
To use a dictionary as an ordered set to filter out duplicate elements while maintaining order, emulate an ordered set.
To use the Ordered Set in Python, use the ordered-set package. There are other packages available as well.
- ordered-set [Python-based]
- orderedset [CPython based]
- collections-extended
- boltons [under iterutils.IndexedSet, Python-based]
- oset
- sortedcontainers
OrderedSet using boltons in Python
To work with boltons IndexSet, you need to install the boltons package.
python3 -m pip install boltons
Now, import the module using the following code.
from boltons.setutils import IndexedSet
Let’s use the IndexedSet and pass the two lists as arguments.
from boltons.setutils import IndexedSet data = IndexedSet[list[range[2]] + list[range[3, 6]]] print[data]
Output
IndexedSet[[0, 1, 3, 4, 5]]
Now, let’s change the order of the lists while passing it to the IndexedSet.
from boltons.setutils import IndexedSet data = IndexedSet[list[range[3, 6]] + list[range[2]]] print[data]
Output
IndexedSet[[3, 4, 5, 0, 1]]
You can see that the order of the set is preserved perfectly. It retains the order as they were added to the Python set.
OrderedSet in Python using sortedcontainers
Python sortedcontainers module provides a SortedSet for preserving the order of the set elements. There are some benefits of using sortedcontainers, including pure Python, fast-as-C implementations, 100% unit test coverage, and hours of stress testing.
Let’s install the sortedcontainers module using the pip package manager.
python3 -m pip install sortedcontainers
Now, import the module using the following code.
from sortedcontainers import SortedSet
Let’s use the SortedSet[] method and pass the two lists as arguments.
from sortedcontainers import SortedSet data = SortedSet[list[range[2]] + list[range[3, 6]]] print[data]
Output
SortedSet[[0, 1, 3, 4, 5]]
You can see that the returns set is sorted in ascending order by default.
Now, let’s change the order of the lists while passing it to the SortedSet.
from sortedcontainers import SortedSet data = SortedSet[list[range[3, 6]] + list[range[2]]] print[data]
Output
SortedSet[[0, 1, 3, 4, 5]]
You can see that even if we add elements to the set in a different order, it will return in the ascending order by default.
That is it for OrderedSet in Python.
Related posts
Python set contains
Python set add
Python set discard