Python Tuple Data Structure in Depth

By | 2018-11-16T01:34:54+00:00 April 16th, 2018|Python|0 Comments

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

Python Tuple Data Structure:

  • The Tuple is an exactly same as 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 tuple.
  • Like List data structure, Tuple also preserves the insertion order.
  • Tuple allows duplicate elements.
  • Tuple allows heterogeneous elements.
  • You can represent a Tuple element by defining within the parenthesis () and each element seperated with 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 to update 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 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 a tuple elements like between the indexes. The slice operator helps us to read values from the tuple for a given index range.

Syntax:

tuple[start:end:step]
  • start โ€“ indicates that the index position, where the atual 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 give 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 occurrance 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 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 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 +’ 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 repeate the tuple elements multiple times using ‘*’ 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:

Python3 Data structure

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