AppDividend
Latest Code Tutorials

How to Append Element to Python Set

0

Appending or adding new elements to the existing set is the most often used operation in Python. If you want to use a single element to the set, use the set.add() method. If you want to add multiple elements or list, or tuple, use the set.update() method.

Python Set append

To append a single element in Python Set, use the add() method. The set add() is an inbuilt Python method that adds a given element to a set if the element is not present in the set.

Syntax

set.add(element)

Parameters

The set add() takes an element parameter, which needs to be added to the set.

Return Value

The add() method doesn’t return any value.

Example of appending an element in Python Set

To define a set in Python, use the set() method.

data = {"Snyder", "Cut", "Trailer", "Is"}
print(data)
data.add("Here")
print("After appending an element")
print(data)

Output

{'Cut', 'Is', 'Trailer', 'Snyder'}
After appending an element
{'Is', 'Snyder', 'Cut', 'Trailer', 'Here'}

The output is unordered, and it added the “Here” element to the set. Set does not guarantee to print the items in the same order as they have defined.

The add() method is useful to append-only a single value to the set.

Appending a tuple to Python Set

To define a tuple in Python, use the tuple() method and pass the argument’s elements. Let’s use the add() method to append a tuple to the set.

data = {"Batman", "Joker", "Superman", "Wonder Woman"}

# tuple
tup = ("Flash", "Aquaman")
print(data)

# using add() function to add tuple
data.add(tup)
print("After appending a tuple")
print(data)

Output

{'Batman', 'Wonder Woman', 'Superman', 'Joker'}
After appending a tuple
{'Batman', 'Wonder Woman', ('Flash', 'Aquaman'), 'Superman', 'Joker'}

You can see that the add() method adds a tuple as a single element. If you want to add tuple elements as an individual element of the set, then use the set.update() method.

To append a tuple in the Python set, use the set.update() method. Python set update() is an inbuilt method used to add items from the set to another set.

data = {"Batman", "Joker", "Superman", "Wonder Woman"}
tup = ("Flash", "Aquaman")
print(data)
data.update(tup)
print("After appending a tuple using update() method")
print(data)

Output

{'Wonder Woman', 'Joker', 'Batman', 'Superman'}
After appending a tuple using update() method
{'Joker', 'Aquaman', 'Superman', 'Wonder Woman', 'Flash', 'Batman'}

As you can see that we got the output as we have expected. It added the elements individually in the set.

Appending a list to Python Set

To append a list in the Python set, use the set.update() method. Python set update() is an inbuilt method used to add items from the set to another set.

data = {"Batman", "Joker", "Superman", "Wonder Woman"}
list = ["Flash", "Aquaman"]
print(data)
data.update(list)
print("After appending a list using update() method")
print(data)

Output

{'Wonder Woman', 'Superman', 'Batman', 'Joker'}
After appending a list using update() method
{'Wonder Woman', 'Superman', 'Aquaman', 'Joker', 'Flash', 'Batman'}

Python set append another set.

To append a set in the existing Python set, use the set.update() method.

data = {"Batman", "Joker", "Superman", "Wonder Woman"}
set = {"Flash", "Aquaman"}
print(data)
data.update(set)
print("After appending a set using update() method")
print(data)

Output

{'Joker', 'Batman', 'Superman', 'Wonder Woman'}
After appending a set using update() method
{'Aquaman', 'Flash', 'Wonder Woman', 'Joker', 'Superman', 'Batman'}

That is it for the Python set add tutorial.

See also

Python set to tuple

Python set to json

Python set to dictionary

Leave A Reply

Your email address will not be published.

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