Add a List to a Set in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Add a List to a Set in Python #

Use the set.update() method to add the elements of a list to a set, e.g. my_set.update(my_list). The set.update() method can be passed an iterable, such as a list, and adds the elements of the list to the set object.

main.py
# ✅ Add the elements of a list to a set (using update()) my_set = {'bobby', 'hadz', 'com'} my_list = ['one', 'two', 'three'] my_set.update(my_list) print(my_set) # 👉️ {'three', 'one', 'com', 'hadz', 'two', 'bobby'} # -------------------------------------------- # ✅ add an actual list to a set (using tuple()) my_set = {('bobby', 'hadz', 'com')} my_list = ['one', 'two', 'three'] my_set.add(tuple(my_list)) print(my_set) # 👉️ {('bobby', 'hadz', 'com'), ('one', 'two', 'three')}

The first example shows how to use the set.update method to add the elements of a list to a set object.

The set.update() method can be passed one or more iterables and updates the set, adding the elements from the iterables.
main.py
my_set = {'bobby', 'hadz', 'com'} my_list = ['one', 'two', 'three'] my_set.update(my_list) print(my_set) # {'three', 'one', 'com', 'hadz', 'two', 'bobby'}

If you need to declare an empty set, make sure to use the set() class because using an empty set of curly braces declares a dictionary.

main.py
my_set = set() # 👈️ declares an empty set my_list = ['bobby', 'hadz', 'com'] my_set.update(my_list) print(my_set) # 👉️ {'com', 'hadz', 'bobby'}

You have to use the tuple() class if you need to add the actual list to the set object.

To add a list to a set:

  1. Use the tuple() class to convert the list to a tuple.
  2. Use the set.add() method to add the tuple to the set.
  3. Tuples are immutable and can be added to a set.
main.py
my_set = {('bobby', 'hadz', 'com')} my_list = ['one', 'two', 'three'] my_set.add(tuple(my_list)) print(my_set) # 👉️ {('bobby', 'hadz', 'com'), ('one', 'two', 'three')}

We can't use a list as an element in a set because list objects are mutable and unhashable.

On the other hand, tuple objects are immutable and hashable.

The set.add method adds the provided element to the set.

main.py
my_set = set() my_set.add('bobby') my_set.add('hadz') my_set.add('com') print(my_set) # 👉️ {'com', 'hadz', 'bobby'}

Most of the immutable built-in objects in Python are hashable, whereas mutable objects are unhashable.

If an object is hashable, then it can be used as an element in a set and as a key in a dictionary because these data structures use the hash value internally.

Hashable objects include - str, int, bool, tuple, frozenset.

Unhashable objects include - list, dict, set.

Note that tuples and frozensets are only hashable if their elements are hashable.

You can check if an object is hashable by passing it to the built-in hash() function.

main.py
print(hash('bobbyhadz.com')) # 👉️ -7027138092780560788 # ⛔️ TypeError: unhashable type: 'list' print(hash(['bobby', 'hadz', 'com']))

The hash function returns the hash value of the passed-in object (if it has one).

Hash values are integers and are used to compare dictionary keys during a dictionary lookup.

Hashable objects have a hash value that never changes during their lifetime. This is why most immutable objects are hashable, whereas mutable ones are unhashable.

Objects like lists are mutable because the contents of a list can be changed.

main.py
my_list = ['bobby', 'hadz', 'com'] my_list[0] = 'new' print(my_list) # ['new', 'hadz', 'com']

On the other hand, tuples that contain primitive values are immutable (and hashable).

main.py
my_tuple = ('bobby', 'hadz', 'com') # ⛔️ TypeError: 'tuple' object does not support item assignment my_tuple[0] = 'new'

The elements in a set object can be any immutable type.

Similarly, dictionaries are indexed by keys and the keys in a dictionary can be any immutable type, e.g. strings or numbers.

Tuples can only be used as elements in a set and as keys in a dictionary if they contain strings, numbers or tuples.

If a tuple contains mutable objects such as lists, it cannot be used as a key in a dictionary or an element in a set.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.