Python Tuple Data Structure in Depth

By | 2019-04-14T21:04:09+05:30 April 16th, 2018|Python|

In this tutorial, we will see in-depth details about Python Tuple Data Structure.

Python Tuple Data Structure:

  • The Tuple is the same as a List data structure in python, except immutability.
  • A Tuple is an immutable object, i.e. once we create a tuple object, you can not change the content of that tuple.
  • If our data is fixed and never changes, then we should go for the tuple.
  • Like List data structure, Tuple also preserves the insertion order.
  • Tuple allows duplicate elements.
  • Tuple allows for different elements.
  • You can represent a Tuple element by defining within the parenthesis () and each element separated with a comma (,).

Creating Python Tuple:

Below are the valid tuple syntaxes.

tuple1= (10,20,30,40,50)
print(type(tuple1))
tuple2=()
print(type(tuple2))
tuple3= 10,20,30
print(type(tuple3))
tuple4= (10,)
print(type(tuple4))

Output:

<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>

Updating Tuple Elements:

As tuple is an immutable data structure, you can not allow updating tuple elements.

Accessing Tuple Elements :

As Tuple is an index based data structure, you can access the elements either by using index or slice operator just like in List data structure.

Accessing Tuple by using the index :

You can traverse the tuple either both directions, a negative index represents the backward direction of traversing, and a positive index represents forward direction.

Python List Data Structure Indexing

Example:

tuple1= (10,20,30,40,50)
print("0th Index : ",tuple1[0])
print("4th Index : ",tuple1[4])
print("-1 th Index : ",tuple1[-1])
print("-3 rd Index : ",tuple1[-3])

Output:

0th Index :  10
4th Index :  50
-1 th Index :  50
-3 rd Index :  30

Accessing Tuple by using slice (:) operator:

You can access the tuple elements even between the indexes. The slice operator helps you to read values from the tuple for a given index range.

Syntax:

tuple[start:end:step]
  • start – indicates that the index position, where the actual slice has to start, the default value is 0
  • end – indicates that the index position, where the slicing has to end, the default value is the length of the tuple
  • step – is an increment value, the default is 1

Example:

tuple1 = (10,20,50,30,40,60,70,80,90)
print("Between 2 and 7 indexes increment by 1 : ",tuple1[2:7:1])
print("Between 2 and 7 indexes increment by 2 : ",tuple1[2:7:2])
print("Between 2 and 7 indexes : ",tuple1[2:7])

Output:

Between 2 and 7 indexes increment by 1 :  (50, 30, 40, 60, 70)
Between 2 and 7 indexes increment by 2 :  (50, 40, 70)
Between 2 and 7 indexes :  (50, 30, 40, 60, 70)

Note: start includes the index whereas end excludes the index.

Tuple traversing using loops:

tuple = (10,20,50,30,)
# Using For loop
print("for loop")
for i in tuple:
    print(i)
#Using while loop
print("while Loop")
i=0
while i<len(tuple):
    print(tuple[i])
    i=i+1

Output:

for loop
10
20
50
30
while Loop
10
20
50
30

Functions on Python Tuple:

  • len() function is used to get the number of elements present in the tuple.
tuple = (10,20,50,30,40,60,70,80,90)
print("Length of the tuple : ",len(tuple))

Output:

Length of the tuple :  9
  • count() function is used to get the number of occurrences of a given element in the tuple.
tuple = (10,20,50,30,50,60,10,80,50)
print("Count of the 50 in tuple : ",tuple.count(50))

Output:

Count of the 50 in tuple :  3
  • index() function is used to get the first occurrence of the given element. If given index is not found, it will throw the ValueError.
tuple = (10,20,50,30,50,60,10,80,50)
print("Count of the 50 in tuple : ",tuple.index(50))

Output:

Count of the 50 in tuple :  2
  • sorted() function is used to sort the elements in tuple based on default (natural) sorting order.
tuple = (10,20,50,30,60,40)
print("Before :",tuple)
tuple2 = sorted(tuple)
print("After :",tuple2)

Output:

Before : (10, 20, 50, 30, 60, 40)
After : [10, 20, 30, 40, 50, 60]

If you wish to sort according to the reverse of default natural sorting order: then you can use the reverse=True parameter.

tuple = (10,20,50,30,60,40)
print("Before :",tuple)
tuple2 = sorted(tuple, reverse=True)
print("After :",tuple2)

Output:

Before : (10, 20, 50, 30, 60, 40)
After : [60, 50, 40, 30, 20, 10]
  • Python min() and max() functions are used to get the min and max values of the tuple.
tuple = (10,20,50,30,60,40)
print("Min is : ",min(tuple)," and Max is : ",max(tuple))

Output:

Min is :  10  and Max is :  60

Concatenating two Tuples :

You can concatenate two tuples using the +’ operator.

tuple1 = (1,2,3,4,5)
tuple2 = (6,7,8,9)
print("New Tuple : ",tuple1+tuple2)

Output:

New Tuple :  (1, 2, 3, 4, 5, 6, 7, 8, 9)

Repeating Tuple Elements:

You can repeat the tuple elements multiple times using the ‘*’ operator.

tuple1 = (1,2,3,4,5)
print("New Tuple : ",tuple1*3)

Output:

New Tuple :  (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

Reference:

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment