You are currently viewing Python Add List to Set with Examples

How to add a list to set in Python? To add elements from the list to the set you can use the update(), union, and add() with for loop. And to add the whole list as a single element to the set you need to convert the list to the set and add it by using add().

Advertisements

Methods to add list to set in Python

  • Use update() to add elements from the list to set in-place
  • Use | or union() to add elements to the set. This returns a new set with updated elements
  • Use for loop with add() to update elements to set iteratively.
  • To add the whole list as a single element to set use the add(). To use this you need to convert a list to a tuple.

1. Quick Examples of Adding List to Set

Following are quick examples of adding elements from a list to a set.


# Quick examples of adding list to set

# Add list to set
myset.update(mylist)

# Add list to set using | operator
myset = myset | set(mylist)

# Add list to set using union()
myset = myset.union(mylist)

# Add list to set using looping
for item in mylist:
    myset.add(item)

# Add whole list to set
myset.add(tuple(mylist))
print("Updated Set: ",myset)

2. Add List to Set in Python using update()

To add a list to a set in Python you can use the update() method from the set, this method takes the list as an argument and adds the elements from the list to the set. This method applies that operation in place meaning it adds the elements from the list to the original set.

Note that the set contains only unique elements, so only items that were not present in the set will be added to the set and duplicate items will not be added.


# Consider set & list
myset={1,2,3,4}
mylist=[6,7,8]
print("Set: ",myset)
print("List: ",mylist)

# Add list to set
myset.update(mylist)
print("Updated Set: ",myset)

This example yields the below output.

python add list to set

2. Using | Operator to add List to Set

The | performs a union operation between pairs of objects in Python, you can use this to add elements of a list to a set. Here the | operator implements the set union operation.


# Consider set & list
myset={1,2,3,4}
mylist=[6,7,8]
print("Set: ",myset)
print("List: ",mylist)

# Add list to set
myset = myset | set(mylist)
print("Updates Set: ",myset)

This example also yields the same output. You can also write the union operator as below short form. Here, |= performs an in-place operation between pairs of objects.


# Add list to set
myset |= set(mylist)

3. Using union() Method

Alternatively, you can also use the union() method. This method takes the list as an argument and returns the set after adding elements to it. Note that this doesn’t update the existing list instead it returns a new list. To update on the existing list, just assign the result back to the existing set variable.


# Consider set & list
myset={1,2,3,4}
mylist=[6,7,8]
print("Set: ",myset)
print("List: ",mylist)

# Add list to set using union()
myset = myset.union(mylist)
print("Updates Set: ",myset)

This yields the same output as above.

4. Add List to Set using Looping

Loop through the python list and get the element for each iteration and add the element to the set using add() method. Here, the add() method updates the set with the new value from the iteration.


# Consider set & list
myset={1,2,3,4}
mylist=[6,7,8]
print("Set: ",myset)
print("List: ",mylist)

# Add list to set using looping
for item in mylist:
    myset.add(item)
print("Updates Set: ",myset)

5. Add Whole List to Set

The above examples add each element from the list into a set however, if you wanted to add the whole list as a single element to the set it is not possible as lists are mutable and are not hashable. You need to convert the list to a tuple first before adding to set.


# Consider set & list
myset={1,2,3,4}
mylist=[6,7,8]
print("Set: ",myset)
print("List: ",mylist)

# Add whole list to set
myset.add(tuple(mylist))
print("Updated Set: ",myset)

This example yields the below output.

python add list elements to set

Conclusion

In this article, you have learned how to add elements from a list to a set in Python. Use update() to add elements from the list to set in place. Use | or union() to add elements to the set, this returns a new set with updated elements. Use for loop with add() to update elements to set iteratively. To add the whole list as a single element to set use the add(). To use this you need to convert a list to a tuple.