The Python Oracle

Add list to set

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles

--

Chapters
00:00 Question
00:15 Accepted answer (Score 250)
01:52 Answer 2 (Score 850)
02:34 Answer 3 (Score 98)
03:21 Answer 4 (Score 46)
03:37 Thank you

--

Full question
https://stackoverflow.com/questions/1306...

Accepted answer links:
[Wikipedia article]: http://en.wikipedia.org/wiki/Hash_tree
[effbot.org]: http://effbot.org/zone/python-hash.htm
[python reference]: http://docs.python.org/reference/datamod...
[recipes]: https://stackoverflow.com/questions/1151...

Answer 2 links:
[set.update()]: https://docs.python.org/3/library/stdtyp...
[hashable]: https://stackoverflow.com/questions/1453...

Answer 3 links:
https://docs.python.org/2/library/sets.h...
[TypeError: unhashable type: 'list' when using built-in set function]: https://stackoverflow.com/questions/1346...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #list #set

#avk47



ANSWER 1

Score 929


Adding the contents of a list

Use set.update() or the |= operator:

>>> a = set('abc')
>>> a
{'a', 'b', 'c'}

>>> xs = ['d', 'e']
>>> a.update(xs)
>>> a
{'e', 'b', 'c', 'd', 'a'}

>>> xs = ['f', 'g']
>>> a |= set(xs)
>>> a
{'e', 'b', 'f', 'c', 'd', 'g', 'a'}

Adding the list itself

It is not possible to directly add the list itself to the set, since set elements must be hashable.

Instead, one may convert the list to a tuple first:

>>> a = {('a', 'b', 'c')}

>>> xs = ['d', 'e']
>>> a.add(tuple(xs))
>>> a
{('a', 'b', 'c'), ('d', 'e')}



ACCEPTED ANSWER

Score 253


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.



ANSWER 3

Score 46


Hopefully this helps:

>>> seta = set('1234')
>>> listb = ['a','b','c']
>>> seta.union(listb)
set(['a', 'c', 'b', '1', '3', '2', '4'])
>>> seta
set(['1', '3', '2', '4'])
>>> seta = seta.union(listb)
>>> seta
set(['a', 'c', 'b', '1', '3', '2', '4'])



ANSWER 4

Score 17


Please notice the function set.update(). The documentation says:

Update a set with the union of itself and others.