Add List to Set

Add list to set

You can't add a list to a set because lists are mutable, meaning that you can change the contents of the list after adding it to the set.

You can however add tuples to the set, because you cannot change the contents of a tuple:

>>> a.add(('f', 'g'))
>>> print a
set(['a', 'c', 'b', 'e', 'd', ('f', 'g')])

Edit: some explanation: The documentation defines a set as an unordered collection of distinct hashable objects. The objects have to be hashable so that finding, adding and removing elements can be done faster than looking at each individual element every time you perform these operations. The specific algorithms used are explained in the Wikipedia article. Pythons hashing algorithms are explained on effbot.org and pythons __hash__ function in the python reference.

Some facts:

  • Set elements as well as dictionary keys have to be hashable
  • Some unhashable datatypes:

    • list: use tuple instead
    • set: use frozenset instead
    • dict: has no official counterpart, but there are some
      recipes
  • Object instances are hashable by default with each instance having a unique hash. You can override this behavior as explained in the python reference.

Better to add item to a set, or convert final list to a set?

TL;DR: Go with option 2. Just use sets from the start.

In Python, sets are hash-sets, and lists are dynamic arrays. Inserting is O(1) for both, but checking if an element exists is O(n) for the list and O(1) for the set.

So option 1 is immediately out. If you are inserting n items and need to check the list every time, then the overall complexity becomes O(n^2).

Options 2 and 3 are both optimal at O(n) overall. Option 2 might be faster in micro-benchnarks because you don't need to move objects between collections. In practice, choose the option that is easier to read and maintain in your specific circumstance.

Append values to a set in Python

your_set.update(your_sequence_of_values)

e.g, your_set.update([1, 2, 3, 4]). Or, if you have to produce the values in a loop for some other reason,

for value in ...:
your_set.add(value)

But, of course, doing it in bulk with a single .update call is faster and handier, when otherwise feasible.

Add List to Set is Possible in Java but not in Python?

Lists are unhashable in python because they do not define __hash__. There are reasons for that, but you could conceivably define your own mutable class that does, provided that said hash does not change across the lifetime of the object. For example, using id(x) as the hash. In fact, you've probably unknowingly implemented exactly that in a lot of your own classes.

I'll quote from the docs on "hashable":

All of Python’s immutable built-in objects are hashable, while no
mutable containers (such as lists or dictionaries) are. Objects which
are instances of user-defined classes are hashable by default; they
all compare unequal (except with themselves), and their hash value is
their id().

Mutability and hashability are coupled, but not at all the same thing.

In Python, how do I add all the elements of a list to a set?

You can use

new_var = set(list_var)

If you want to do it iteratively you can use:

set_var = set()
for item in iterable:
set_var.add(item)

How to construct a set out of list items in python?

If you have a list of hashable objects (filenames would probably be strings, so they should count):

lst = ['foo.py', 'bar.py', 'baz.py', 'qux.py', Ellipsis]

you can construct the set directly:

s = set(lst)

In fact, set will work this way with any iterable object! (Isn't duck typing great?)


If you want to do it iteratively:

s = set()
for item in iterable:
s.add(item)

But there's rarely a need to do it this way. I only mention it because the set.add method is quite useful.

Python list.append if not in list vs set.add performance

The set is far faster, in general. Testing for membership in a list is O(n), linear in the size of the list. Adding to a set is O(1), independent of the number of the items in the list. Aside from that, the list code makes two function calls: one to check if 12 is in the list, another to add it, while the set operation makes just one call.

Note that the list solution can be fast, though, when the item doesn't need to be added to the list because it was found early in the list.

# Add item to set
$ python -m timeit -s 's = set(range(100))' 's.add(101)'
10000000 loops, best of 3: 0.0619 usec per loop

# Add item not found in list
$ python -m timeit -s 'l = list(range(100))' 'if 101 not in l: l.append(101)'
1000000 loops, best of 3: 1.23 usec per loop

# "Add" item found early in list
$ python -m timeit -s 'l = list(range(100))' 'if 0 not in l: l.append(0)'
10000000 loops, best of 3: 0.0214 usec per loop

# "Add" item found at the end of the list
$ python -m timeit -s 'l = list(range(102))' 'if 101 not in l: l.append(101)'
1000000 loops, best of 3: 1.24 usec per loop

Append elements of a set to a list in Python

Use

a.extend(list(b))

or even easier

a.extend(b)

instead.



Related Topics



Leave a reply



Submit