Python Tuple: Different ways to create a tuple and Iterate over it

In this article we will discuss what is tuple, how its different from list, different ways to create a tuple and iterate over it.

What is a tuple ?

Tuple is a kind of heterogeneous sequential container that can store elements of different types. Tuples are immutable i.e once created then we can not change it’s contents.
A sample tuple is as follows,

tupleObj = ('Riti', 31, 'London', 78.88)

Unlike list, elements in tuple are wrapped using braces i.e. ‘(‘ & ‘)’

Difference between list and tuple

  • List is mutable i.e once created then we can modify its contents. Whereas, a Tuple is immutable i.e. once created we can change its contents, therefore tuple doesn’t provide functions like append(), remove() etc.
  • As tuple is immutable, so we can use it as key in a dictionary too.

Different ways to create a tuple

Create a tuple of different type of elements

We can create a tuple of different type of elements by just separating them with ‘,’ and wrapping them using braces i.e.

tupleObj = ('Riti', 31, 'London', 78.88)

print(tupleObj)

Output:
('Riti', 31, 'London', 78.88)

Create a tuple from unpacked elements

We can also create a tuple from unpacked elements i.e without any braces just elements separated by comma i.e.

tupleObj = 'Jake', 23, 'paris' , 89.33

print(tupleObj)

Output:
('Jake', 23, 'paris', 89.33)

Create an empty tuple

We can also create an empty tuple object like this,

# Creating an Empty Tuple
emptyTuple = ()

Advertisements

Create a tuple from a list

We can also convert a list or any other sequential container to tuple by just type casting.
Suppose we have a list of int,

# List of numbers
listOfNumbers = [12 , 34, 45, 22, 33 ]

Let’s create a tuple from this list,
# Create a tuple from list by type casting
tupleObj = tuple(listOfNumbers)

print(tupleObj)

Output:
(12, 34, 45, 22, 33)

Iterate over a tuple

We can iterate over a tuple using for loop and in operator just like any other container in python i.e.

tupleObj = ('Riti', 31, 'London', 78.88)

for elem in tupleObj:
    print(elem)

Output:
Riti
31
London
78.88

Complete example is as follows,
def main():
    
    print("****** Different ways to create Tuple ******")
    
    # Creating an Empty Tuple
    emptyTuple = ()
    
    print(emptyTuple)
    
    print("Create tuple from different type of elements")
    
    tupleObj = ('Riti', 31, 'London', 78.88)
    
    print(tupleObj)

    print("Create tuple from unpacked elements ")
        
    tupleObj = 'Jake', 23, 'paris' , 89.33
    
    print(tupleObj)
    
    print("Create tuple from List ")
    
    # List of numbers
    listOfNumbers = [12 , 34, 45, 22, 33 ]
    
    # Create a tuple from list by type casting
    tupleObj = tuple(listOfNumbers)
    
    print(tupleObj)
    
    print("*********** Iterate over tuple ***********")
    
    tupleObj = ('Riti', 31, 'London', 78.88)
    
    for elem in tupleObj:
        print(elem)

           
if __name__ == '__main__':
    main()


Output:
****** Different ways to create Tuple ******
()
Create tuple from different type of elements
('Riti', 31, 'London', 78.88)
Create tuple from unpacked elements 
('Jake', 23, 'paris', 89.33)
Create tuple from List 
(12, 34, 45, 22, 33)
*********** Iterate over tuple ***********
Riti
31
London
78.88

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top