set.add() Function in Python

The “Set” is one of the inbuilt data types used to store large collections of data. Unlike lists and dictionaries, the “Set” elements are not arranged in order, are unchangeable, and the element values are unindexed. To add a new element in a set, the function named “set.add()” is utilized in the Python program.

This write-up will provide a comprehensive Python guide on the set.add() function with multiple examples. The following outcomes are explained in this blog:

So let’s begin our Python tutorial!

What is Python set.add()?

Python provides a built-in function named “set.add()” that adds a new element to a set. The set.add() function accepts exactly one argument. If you pass multiple arguments to the set.add() function, then you will encounter an error. The syntax of the “set.add()” is shown below:

set.add(element)

The parameter “element” takes the input value that must be added to the set. The “set.add()” does not return any value while executing it only adds the element value to a set.

Let’s understand the Python set.add() function with the examples shown below:

Example 1: Using Set add() to Add the New Element to a Set

In the following code, the “set.add()” function takes the new element value as an argument and adds it into the set value.

Code:

# set of names
set_value = {'Alex', 'John', 'Lily'}

# adding new name 'David'
set_value.add('David')
print("New Set Value: ", set_value)

In the above code snippet:

  • The set value is initialized in a variable named “set_value”.
  • To add a new item/element in the given set, the “set.add()” function is used in the above code. The element value will be passed as an argument of the function “set.add()”.
  • The updated set value will be shown on the console screen.

Output:

In the above output, the element “David” is added to the set.

Example 2: Using Set add() to Add the Same Element to a Set

In the example shown below, the “set.add()” function is used to add the same element value into the set.

Code:

# set of numbers
set_value = {12, 10, 14,42}

# adding same element 12 to a set
set_value.add(12)
print("Set Value: ",set_value)

In the above mentioned code:

  • The set is initialized with the numeric values.
  • The same value, i.e., “12”, is added to the set using the “set.add()” function.
  • The set.add() function doesn’t add the duplicates.
  • So, if we add an already existing element to a set, then nothing will happen to the original set.

Output:

This output confirms that the add() function does not add duplicate values.

Example 3: Using set.add() to Add any Iterable to a Set

In the example shown below, the “set.add()” function is used to add a tuple to the given set:

Code:

Set_value = {'Alex', 'John', 'David', 'Lily', 'Joseph'}
tuple_value = ('Alex', 'Smith')

# adding tuple to set
Set_value.add(tuple_value)
print('\nAdding tuple to set: ',Set_value)

In the above example code:

  • The set and tuple values are initialized in the program.
  • The “set.add()” function adds a tuple to the set.

Output:

From the output, it is verified that the tuple is added to the set.

In Python, the “set.add()” function retrieves “unhashable type errors” while adding a list or dictionary. To avoid such an error, the function “set.update()” can be used:

Code:

Set_value = {'Alex', 'John', 'David', 'Lily', 'Joseph'}
list_value=['Alex','Miller']
# adding list to set
Set_value.update(list_value)
print('\nAdding List to set: ',Set_value)
  • In this example, a set and a list is initialized in the program.
  • The inbuilt “set.update()” function is used to add the list to the set.

Output:

The output shows that the list values have been added to the set successfully.

This concludes our Python set.add() guide!

Conclusion

To add a new element into a “Set”, a built-in function named “set.add()” is used in Python. In Python, the “set.add()” function retrieves “unhashable type errors” while adding a list or dictionary. To avoid such an error, the function “set.update()” can be used. This Python guide explained all the details regarding the set.add() function with numerous examples.