python

How to Join Tuples in Python


In this article, you will learn how to join tuples in Python.

Let’s say you have 2 tuples.

# A tuple named 'a' with value ("Red", "Green", "Blue")
a = ("Red", "Green", "Blue")

# A tuple named 'b' with value ("Cyan", "Yellow", "Magenta")
b = ("Cyan", "Yellow", "Magenta") 

Join Tuples in Python

In order to join tuples, you can use the zip() method and tuple() method.

# A tuple named 'a' with value ("Red", "Green", "Blue")
a = ("Red", "Green", "Blue")

# A tuple named 'b' with value ("Cyan", "Yellow", "Magenta")
b = ("Cyan", "Yellow", "Magenta")

# Display tuples in readable format
print(tuple(zip(a, b)))
# (('Red', 'Cyan'), ('Green', 'Yellow'), ('Blue', 'Magenta'))

Note: The zip() method functions by creating a series of tuples from each element in both supplied iterables. The tuple() method functions by creating a tuple from the supplied iterable.


Share on social media

//