7 Ways to add all elements of list to set in python

In this article we will discuss 7 different ways to add all elements of a list to the set in python.

Suppose we have a list and a set i.e.

# Set of numbers
sample_set = {11, 12, 13, 14}

# List of numbers
list_of_num = [10, 11, 12, 13, 14, 15, 16]

Now we want to add all the elements of the list to the set. As set contains only unique elements, so after adding elements from a list to the set, the contents of set should be like,

{10, 11, 12, 13, 14, 15, 16}

There are different ways to do this and we will discuss them one by one,

Add all elements of a list to set using update() function

In python, the set class provides a member function update() i.e.

set.update(sequences)

It accepts a single or multiple iterable sequences as arguments and adds all the elements in these sequences to the set.

We can use this update() function to add all elements from a list to the set i.e.

# Create and intialize a set
sample_set = {11, 12, 13, 14}

# a list of numbers
list_of_num = [10, 11, 12, 13, 14, 15, 16]

# add all elements in list to the set
sample_set.update(list_of_num)

print('Modified Set: ')
print(sample_set)

Output:

Modified Set: 
{10, 11, 12, 13, 14, 15, 16}

We passed a list as an argument to the update() function. It added all the items in the list to the set. The set contains only unique elements, so items which were not present in the set got added and duplicate items were just skipped.

Adding a list to set using add() function

In python, the set class provides a member function add() i.e.

set.add(element)

It accepts a single element as an argument and adds that element to the set. But that element should be immutable.
If we try to pass a list to the add() function, then it will give error because list is a mutable object,

sample_set.add(list_of_num)

Error

TypeError: unhashable type: 'list'

So to add all items in a list to the set using add() function, we need to use a for loop.

Add all items in list to set using add() & for loop

Iterate over all the items in the list using a for loop and pass each item as an argument to the add() function. If that item is not already present in the set,
then it will be added to the set i.e.

# A set of numbers
sample_set = {11, 12, 13, 14}

# a list of numbers
list_of_num = [10, 11, 12, 13, 14, 15, 16]

# Iterate over all elements of list and
for elem in list_of_num:
    # add each element to the set
    sample_set.add(elem)

print('Modified Set: ')
print(sample_set)

Output:

Modified Set: 
{10, 11, 12, 13, 14, 15, 16}

Add a list to set using add() & union()

In python, set class provides a function to add the contents of two sets i.e.

s.union(t)

It returns a new set with elements from both s and t.

We can use this to add all elements of a list to the set i.e.

sample_set = {11, 12, 13, 14}
list_of_num = [10, 11, 12, 13, 14, 15, 16]

# convert list to set and get union of both the sets
sample_set = sample_set.union(set(list_of_num))

print('Modified Set: ')
print(sample_set)

Output:

Modified Set: 
{10, 11, 12, 13, 14, 15, 16}

We converted our list to the set and passed it to the union() function as an argument. union() function returns a set that contains items from both the set i.e. our set and the list (which we converted to the set). As a set contains only unique elements, therefore duplicate elements were just ignored.

Add all elements in a list to set using | operator

We can take union of two sets using | operator too. So, just like the previous solution, we will convert our list to a set and then create a union of both the sets
using | operator i.e.

sample_set = {11, 12, 13, 14}
list_of_num = [10, 11, 12, 13, 14, 15, 16]

# convert list to set and get union of both the sets using |
sample_set |= set(list_of_num)

print('Modified Set: ')
print(sample_set)

Output:

Modified Set: 
{10, 11, 12, 13, 14, 15, 16}

Add a list to set using |= and unpacking list to set

Just like the previous solution, we will take a union of two sets. But to convert our list to a set, we will use string literal and unpack our lists elements inside it,

sample_set = {11, 12, 13, 14}
list_of_num = [10, 11, 12, 13, 14, 15, 16]

# unpack list to a set and OR that with original set
sample_set |= {*list_of_num}

print('Modified Set: ')
print(sample_set)

Output:

Modified Set: 
{10, 11, 12, 13, 14, 15, 16}

It added all the items in our list to the set. Now our set contains elements from both the original set and list. Duplicate elements were just skipped.

Adding all elements from multiple lists to the set

Suppose we have 3 different lists,

# 3 lists of numbers
list_num_1 = [15, 16, 17]
list_num_2 = [18, 19]
list_num_3 = [30, 31, 19, 17]

Now to add all from these lists to the set, we can use update() function,

# A set of numbers
sample_set = {11, 12, 13, 14}

# Add multiple lists
sample_set.update(list_num_1, list_num_2, list_num_3)

print('Modified Set: ')
print(sample_set)

Output:

Modified Set: 
{11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31}

In update() function, we can pass multiple iterable sequences as arguments and it adds all the items in these sequences to the set. So, here we passed three lists to the update() function and it added all the elements in these lists to the set.

The complete example is as follows,

def main():
    print('*** Add all elements of list to set using update() function ***')

    # Create and intialize a set
    sample_set = {11, 12, 13, 14}

    # a list of numbers
    list_of_num = [10, 11, 12, 13, 14, 15, 16]

    # add all elements in list to the set
    sample_set.update(list_of_num)

    print('Modified Set: ')
    print(sample_set)

    print('*** Adding a list to set using add() function ***')

    sample_set = {11, 12, 13, 14}

    # a list of numbers
    list_of_num = [10, 11, 12, 13, 14, 15, 16]

    # Wrong Way
    # Error: TypeError: unhashable type: 'list'
    # sample_set.add(list_of_num)

    print('Add all items in list to set using add() & for loop')

    # A set of numbers
    sample_set = {11, 12, 13, 14}

    # a list of numbers
    list_of_num = [10, 11, 12, 13, 14, 15, 16]

    # Iterate over all elements of list and
    for elem in list_of_num:
        # add each element to the set
        sample_set.add(elem)

    print('Modified Set: ')
    print(sample_set)

    print('** Add a list to set using add() & union() **')

    # A set of numbers
    sample_set = {11, 12, 13, 14}

    # a list of numbers
    list_of_num = [10, 11, 12, 13, 14, 15, 16]

    # convert list to set and get union of both the sets
    sample_set = sample_set.union(set(list_of_num))

    print('Modified Set: ')
    print(sample_set)

    print('** Add all elements in a list to set using | operator **')

    # A set of numbers
    sample_set = {11, 12, 13, 14}

    # a list of numbers
    list_of_num = [10, 11, 12, 13, 14, 15, 16]

    # convert list to set and get union of both the sets using |
    sample_set |= set(list_of_num)

    print('Modified Set: ')
    print(sample_set)

    print('** Add a list to set using |= and unpacking list to set **')

    sample_set = {11, 12, 13, 14}
    list_of_num = [10, 11, 12, 13, 14, 15, 16]

    # unpack list to a set and OR that with original set
    sample_set |= {*list_of_num}

    print('Modified Set: ')
    print(sample_set)

    print('*** Adding elements from multiple lists to the set ***')

    # A set of numbers
    sample_set = {11, 12, 13, 14}

    # 3 lists of numbers
    list_num_1 = [15, 16, 17]
    list_num_2 = [18, 19]
    list_num_3 = [30, 31, 19, 17]

    # Add multiple lists
    sample_set.update(list_num_1, list_num_2, list_num_3)

    print('Modified Set: ')
    print(sample_set)


if __name__ == '__main__':
   main()

Output:

*** Add all elements of list to set using update() function ***
Modified Set: 
{10, 11, 12, 13, 14, 15, 16}
*** Adding a list to set using add() function ***
Add all items in list to set using add() & for loop
Modified Set: 
{10, 11, 12, 13, 14, 15, 16}
** Add a list to set using add() & union() **
Modified Set: 
{10, 11, 12, 13, 14, 15, 16}
** Add all elements in a list to set using | operator **
Modified Set: 
{10, 11, 12, 13, 14, 15, 16}
** Add a list to set using |= and unpacking list to set **
Modified Set: 
{10, 11, 12, 13, 14, 15, 16}
*** Adding elements from multiple lists to the set ***
Modified Set: 
{11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31}

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top