This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
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.
The post How to Join Tuples in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-26T13:49:48+00:00) How to Join Tuples in Python. Retrieved from https://www.scien.cx/2021/02/26/how-to-join-tuples-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.