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 = ()

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

 

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top