How to add to a set in python?

Set is a collection of data items, separated by commas and enclosed within curly brackets {}. Set is unordered, mutable, unindexed, and doesn't allow redundant data. For eg, set_1 = {1, 2, "red", "black", 3, 4} represents a set.

This article discusses different ways to add items to a set in Python.

Adding an item to a set using add() method

The add() is a built-in function in python that adds a single item to the set. The add() function takes an element as a parameter and returns a set. The syntax to add an element in the set using add() method is given below.

#syntax:
set.add(element)

Example 1: Consider a set set_1 = {"apple", 1, 3, 4, "Python"}. set_1.add("red") adds element red to set_1 and returns the updated set as {1, 3, 4, 'Python', 'apple', 'red'}

set_1 = {"apple",1, 3, 4, "Python"}
print("set_1 : ", set_1)
set_1.add("red")
print("The updated set is : ", set_1)

Output

set_1 :  {1, 3, 4, 'Python', 'apple'}
The updated set is :  {1, 3, 4, 'Python', 'apple', 'red'}

Example 2: Adding a tuple (3, 5, 7) to set_1

set_1 = {"apple", 1, 3, 4, "Python"}
print("set_1 : ", set_1)
set_1.add((3, 5, 7))
print("The updated set is : ", set_1)

Output

set_1 :  {1, 3, 4, 'Python', 'apple'}
The updated set is :  {1, 3, 4, 'Python', (3, 5, 7), 'apple'}

If an element is already present in the set, the add() method ignores that element. For example, adding and element 4 to set_1 = {"apple",1, 3, 4, "Python"} returns the same set, as the set doesn't allow duplicate values.

set_1 = {"apple", 1, 3, 4, "Python"}
print("set_1 : ", set_1)
set_1.add(4)
print("The updated set is : ", set_1)

Output

set_1 :  {1, 'apple', 3, 4, 'Python'}
The updated set is :  {1, 'apple', 3, 4, 'Python'}

Adding an item to a set using the update() method

The update() method adds all the items of an iterable to a set and ignores the duplicate values. The update() method takes a sequence(list, tuple, string, etc) as a parameter and returns a set. The syntax for the update() method is as follows.

#syntax:
set.update(sequence)

Consider a set set_1 = {"apple", 1, 4, "Python"}. set_1.update([3, 5, 7]) adds all the element to set_1 and returns the updated set as {1, 3, 4, 5, 7, 'apple', 'Python'}

set_1 = {"apple", 1, 4, "Python"}
print("set_1 : ", set_1)
set_1.update([3, 5, 7])
print("The updated set is : ", set_1)

Output

set_1 :  {'apple', 1, 'Python', 4}
The updated set is :  {1, 3, 4, 5, 7, 'apple', 'Python'}

When a string is passed as a parameter to update() method, each character of the string is added as an element to the set. If we want the whole string as an element, then the string should be added using add() method.

Example,

set_1 = {"apple", 1, 4, "Python"}
print("set_1 : ", set_1)
set_1.add("abc")
print("Adding a string using add() method : ", set_1)
set_1 = {1, 2, 3}
print("set_1 : ", set_1)
set_1.update("abc")

Output

set_1 :  {'apple', 1, 4, 'Python'}
Adding a string using add() method :  {1, 'abc', 4, 'apple', 'Python'}
set_1 :  {1, 2, 3}
Adding a string using update() method :  {1, 2, 3, 'c', 'a', 'b'}

The update() method can take multiple sequences as a parameter, it will iterate all the sequences and add elements to the set.

Example,

set_1 = {"apple", 1, 4, "Python"}
print("set_1 : ", set_1)
set_1.update("abc", [9,10])
print("The updated set is : ", set_1)

Output

set_1 :  {1, 'Python', 4, 'apple'}
The updated set is :  {1, 4, 'apple', 'Python', 9, 10, 'c', 'a', 'b'}
If a single element is passed as a parameter to update() method it raises the TypeError exception.

Example,

set_1 = {"apple", 1, 4, "Python"}
print("set_1 : ", set_1)
set_1.update(9)
print("The updated set is : ", set_1)

Output

  set_1.update(9)
TypeError: 'int' object is not iterable
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions