python – Quick way to extend a set if we know elements are unique

python – Quick way to extend a set if we know elements are unique

You can use set.update to update your master set in place. This saves allocating a new set all the time so it should be a little faster than set.union

>>> s = set(range(3))
>>> s.update(range(4))
>>> s
set([0, 1, 2, 3])

Of course, if youre doing this in a loop:

masterSet = set()
for setA in iterable:
    masterSet = masterSet.union(setA)

You might get a performance boost by doing something like:

masterSet = set().union(*iterable)

Ultimately, membership testing of a set is O(1) (in the average case), so testing if the element is already contained in the set isnt really a big performance hit.

As mgilson points out, you can use update to update a set in-place from another set. That actually works out slightly quicker:

def union():
    i = set(range(10000))
    j = set(range(5000, 15000))
    return i.union(j)

def update():
    i = set(range(10000))
    j = set(range(5000, 15000))
    i.update(j)
    return i

timeit.Timer(union).timeit(10000)   # 10.351907968521118
timeit.Timer(update).timeit(10000)  # 8.83384895324707

python – Quick way to extend a set if we know elements are unique

If you know your elements are unique, a set is not necessarily the best structure.

A simple list is way faster to extend.

masterList = list(masterSet)
masterList.extend(setA)

Leave a Reply

Your email address will not be published.