AppDividend
Latest Code Tutorials

How to Add List to Set in Python

0

Python set is used to save multiple elements in a single variable. The set does not contain duplicate elements. So, if you want to store unique elements, then use the set data structure. You can add single or multiple elements to the set. Let’s see how to add a list to the set.

Add List to Set in Python

To add a list to set in Python, use the set.update() function. The set.update() is a built-in Python function that accepts a single element or multiple iterable sequences as arguments and adds that to the set.

Syntax

set.update(element or sequence)

Argument

The update() method takes a single element or a sequence as an argument.

Example

Let’s define a set and add that set to a list.

num_set = {11, 21, 19, 18, 46}

# List of numbers
num_list = [111, 118, 119, 200, 121, 129, 146]

# Add all elements of a list to the set
num_set.update(num_list)
print('Modified Set: ')
print(num_set)

Output

Modified Set: {129, 200, 11, 18, 19, 146, 21, 46, 111, 118, 119, 121}

In this example, we passed a list as an argument to the update() function, and it added all the elements in the list to the set.

One thing to remember is that the set contains unique elements. If it finds the duplicate element, then it will remove the duplicates from the set.

So the elements that were not present in the set got added, and duplicate elements were just skipped.

Using add() function and for loop

The set.add() is a built-in function that takes a single element as an argument and adds that element to the set. If you pass the list or other iterables, then it will fail.

Syntax

set.add(element)

Arguments

The set.add() method accepts a single element as an argument and adds that element to the set. But that element must be immutable.

Example

Let’s add a list to set using only the set.add() function and see what we get.

num_set = {11, 21, 19, 18, 46}

# List of numbers
num_list = [111, 118, 119, 200, 121, 129, 146]

# Add all elements of a list to the set
num_set.add(num_list)
print('Modified Set: ')
print(num_set)

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/app.py", line 7, in <module>
    num_set.add(num_list)
TypeError: unhashable type: 'list'

And we got the TypeError: unhashable type: ‘list’. That means we can not use the add() function alone to add a list to set. To get around this, we will use the for loop.

We will iterate over all the elements 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, it will be added to the set.

num_set = {11, 21, 19, 18, 46}

# List of numbers
num_list = [111, 118, 119, 200, 121, 129, 146]

for item in num_list:
    num_set.add(item)
print('Modified Set: ')
print(num_set)

Output

Modified Set: {129, 200, 11, 18, 19, 146, 21, 46, 111, 118, 119, 121}

Using add() and union() functions

The set union() is a built-in Python method that returns a new set with unique elements from all the sets. See the below example.

num_set = {11, 21, 19, 18, 46}

# List of numbers
num_list = [111, 118, 119, 200, 121, 129, 146]

num_set = num_set.union(set(num_list))
print('Modified Set: ', num_set)

Output

Modified Set:  {129, 200, 11, 46, 111, 18, 19, 146, 21, 118, 119, 121}

You can see that we converted our list to the set and passed it to the union() function as an argument.

The union() method returns a set that contains elements from both sets.

Using | operator

The | operator is used to take the union of two sets.

num_set = {11, 21, 19, 18, 46}

# List of numbers
num_list = [111, 118, 119, 200, 121, 129, 146]

num_set |= set(num_list)
print('Modified Set: ', num_set)

Output

Modified Set:  {129, 200, 11, 46, 111, 18, 19, 146, 21, 118, 119, 121}

That is it for this tutorial.

Leave A Reply

Your email address will not be published.

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