How to join two sets in one line without using "|"
PythonSetPython Problem Overview
Assume that S
and T
are assigned sets. Without using the join operator |
, how can I find the union of the two sets? This, for example, finds the intersection:
S = {1, 2, 3, 4}
T = {3, 4, 5, 6}
S_intersect_T = { i for i in S if i in T }
So how can I find the union of two sets in one line without using |
?
Python Solutions
Solution 1 - Python
You can use union method for sets: set.union(other_set)
Note that it returns a new set i.e it doesn't modify itself.
Solution 2 - Python
You could use or_
alias:
>>> from operator import or_
>>> from functools import reduce # python3 required
>>> reduce(or_, [{1, 2, 3, 4}, {3, 4, 5, 6}])
set([1, 2, 3, 4, 5, 6])
Solution 3 - Python
If you are fine with modifying the original set (which you may want to do in some cases), you can use set.update()
:
S.update(T)
The return value is None
, but S
will be updated to be the union of the original S
and T
.
Solution 4 - Python
Assuming you also can't use s.union(t)
, which is equivalent to s | t
, you could try
>>> from itertools import chain
>>> set(chain(s,t))
set([1, 2, 3, 4, 5, 6])
Or, if you want a comprehension,
>>> {i for j in (s,t) for i in j}
set([1, 2, 3, 4, 5, 6])
Solution 5 - Python
If by join you mean union, try this:
set(list(s) + list(t))
It's a bit of a hack, but I can't think of a better one liner to do it.
Solution 6 - Python
You can just unpack both sets into one like this:
>>> set_1 = {1, 2, 3, 4}
>>> set_2 = {3, 4, 5, 6}
>>> union = {*set_1, *set_2}
>>> union
{1, 2, 3, 4, 5, 6}
The *
unpacks the set. Unpacking is where an iterable (e.g. a set or list) is represented as every item it yields. This means the above example simplifies to {1, 2, 3, 4, 3, 4, 5, 6}
which then simplifies to {1, 2, 3, 4, 5, 6}
because the set can only contain unique items.
Solution 7 - Python
Suppose you have 2 lists
A = [1,2,3,4]
B = [3,4,5,6]
so you can find A
Union B
as follow
union = set(A).union(set(B))
also if you want to find intersection and non-intersection you do that as follow
intersection = set(A).intersection(set(B))
non_intersection = union - intersection
Solution 8 - Python
You can do union
or simple list comprehension
[A.add(_) for _ in B]
A would have all the elements of B
Solution 9 - Python
If you want to join n
sets, the best performance seems to be from set().union(*list_of_sets)
, which will return a new set.
Thus, the usage might be:
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s3 = {4, 5, 6}
s1.union(s2, s3) # returns a new set
# Out: {1, 2, 3, 4, 5, 6}
s1.update(s2, s3) # updates inplace
Adding to Alexander Klimenko's answer above, I did some simple testing as shown below. I believe the main takeaway is that it seems like the more random the sets are, the bigger the difference on performance.
from random import randint
n = 100
generate_equal = lambda: set(range(10_000))
generate_random = lambda: {randint(0, 100_000) for _ in range(10_000)}
for l in [
[generate_equal() for _ in range(n)],
[generate_random() for _ in range(n)]
]:
%timeit set().union(*l)
%timeit reduce(or_, l)
Out:
# equal sets: 69.5 / 23.6 =~ 3
23.6 ms ± 658 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
69.5 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# random sets: 438 / 78.7 =~ 5.6
78.7 ms ± 1.48 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
438 ms ± 20.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Therefore, if you want to update inplace, the best performance comes from set.update
method, as, performance wise, s1.update(s2, s3) = set().union(s2, s3)
.