Open In App

Append Tuple To A Set With Tuples

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Tuples and sets in Python have some key differences. Tuples are ordered collections of elements that can’t be changed once created, denoted by parentheses, and they allow duplicates. You can access specific elements using indexing and slicing. On the other hand, sets are unordered collections of unique elements, represented by curly braces. They can be changed after creation, automatically removing duplicates. However, sets don’t support indexing or slicing; you interact with their elements through iteration or specific methods.

Append Tuple To A Set With Tuples

Below, we provide examples to illustrate how to append a tuple to a set with tuples in Python.

Append Tuple To A Set With Tuples Using add() Method

To append a tuple to the set, use the add() method. The add() method is a built-in method in Python used for adding elements to a set. It takes a single argument, the element to be added, and modifies the set accordingly. If the element is already present in the set, the set remains unchanged.

Example: In the following example, we have initially created a set of tuples called my_set and used the set’s add() method to add a new tuple to this set. A new tuple (7,8) added to the set my_set.

Python3




# Append Tuple To A Set With Tuples
my_set = {(1, 2), (3, 4), (5, 6)}
new_tuple = (7, 8)
 
my_set.add(new_tuple)
print("Updated Set:", my_set)


Output

Updated Set: {(1, 2), (3, 4), (5, 6), (7, 8)}

Append Tuple To A Set With Tuples Using update() Method

Instead of appending a single tuple, If you want to append a list of tuples to the set, use the python update() method.

Example : In the following example, we have initially created a set of tuples called my_set and used the update() method to add multiple tuples to this set. A set of tuples {(7,8),(9,10),(11,12)} added to my_set.

Python3




# Append Tuple To A Set With Tuples
my_set = {(1, 2), (3, 4), (5, 6)}
new_tuples = {(7, 8), (9, 10), (11, 12)}
 
my_set.update(new_tuples)
print("Updated Set:", my_set)


Output

Updated Set: {(1, 2), (9, 10), (7, 8), (5, 6), (11, 12), (3, 4)}

Append Tuple To A Set With Tuples Using union() Method

Think of it as a friendly gathering where existing sets join forces with new elements.

Example: In this example, the union() method recieves a tuple as function argument and appends it to the set of tuples my_set.

Python3




# Append Tuple To A Set With Tuples
my_set = {(1, 2), (3, 4), (5, 6)}
new_tuple = (7, 8)
 
updated_set = my_set.union({new_tuple})
print("Updated Set:", updated_set)


Output

Updated Set: {(1, 2), (3, 4), (5, 6), (7, 8)}

Append Tuple To A Set With Tuples Using | (Pipe) Method

In Python, the | operator performs a union operation, combining the elements of two sets.

Example: In this example, the existing set my_set joins hands with a set containing the new tuple using the | operator. It’s a concise and elegant way of achieving the union, providing a visually appealing and straightforward method for appending tuples to sets in Python.

Python3




# Append Tuple To A Set With Tuples
my_set = {(1, 2), (3, 4), (5, 6)}
new_tuple = (7, 8)
 
updated_set = my_set | {new_tuple}
print("Updated Set:", updated_set)


Output

Updated Set: {(1, 2), (3, 4), (5, 6), (7, 8)}

Conclusion

In conclusion, appending tuples to sets in Python boils down to a few simple methods. The add() method is your go-to for adding a single tuple, keeping things neat. If you’ve got multiple tuples or sets to bring together, the union() method is your friend. For a quick and sleek approach, the | (pipe) operator is your shortcut, making the process concise and elegant.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads