Tuple Data Structure in Python

 

Tuple Data Structure in Python

A tuple data structure in python is a sequence of values similar to a list. Values stored in a tuple can be of any type (maybe numeric values, characters, or strings), and they are indexed by integers. The first element is indexed at zero.

The important difference between tuple and list is that tuples are immutable. Tuples are also comparable and hashable so we can sort lists of them and use tuples as key values in Python dictionaries.

Video Tutorial

How to create an Empty tuple or tuple with elements

A tuple can be created using a pair of parenthesis ().

t = ()
print (t)

Output

()
#Tuple is a comma-separated list of values:

t = ('HIT', 'NDS')
print (t)

Output

('HIT', 'NDS')

To create a tuple with a single element, you have to include the final comma:

t = ('HIT',)
print (t)

Output

('HIT',)

If the argument is a sequence (string, list, or tuple), the result of the call to a tuple is a tuple with the elements of the sequence:

t = tuple('HIT NDS')
print(t)

Output

('H', 'I', 'T', ' ', 'N', 'D', 'S')

The bracket operator indexes an element and the slice operator selects a range of elements.

#print using index operator

t = ('a', 'b', 'c', 'd', 'e')
print(t[0])

#print using slicing

t = ('a', 'b', 'c', 'd', 'e')
print(t[1:4])

Output

a
('b', 'c', 'd')

Tuples are immutable. Hence one can’t modify the elements of a tuple data structure in python, but you can replace one tuple with another:

#List is mutable, in this case the element at index 1 is modified.

l1 = ['a', 'b', 'c', 'd', 'e']
print (l1)
l1[1] = 'B'
print (l1)

Output

['a', 'b', 'c', 'd', 'e']
['a', 'B', 'c', 'd', 'e']
#updating tuple

t = ('a', 'b', 'c', 'd', 'e')

t[1] = 'B'

Output

TypeError: 'tuple' object does not support item assignment
# Tuple assignment
t1 = ('a', 'b', 'c', 'd', 'e')
t2 = ('x', 'y', 'z')
t1 = t2
print (t1)

Output

('x', 'y', 'z')

Comparing tuples

The comparison operators work with tuple data structures and other sequences. In python, the element by element comparison takes on the tuple data structure. That is python compares the first element of one tuple with the first element of another tuple. If they are equal, it goes on to the next element of the tuple, and so on, until it finds elements that differ. Once the difference is found, subsequent elements are not considered for comparison (even if they are really big).

t1 = (0, 1, 2) 
t2 = (1, 3, 4)
t1 < t2

Output

True
t1 = (0, 100, 2000000) 
t2 = (1, 3, 4)
t1 < t2

Output

True
t1 = (2, 100, 2000000) 
t2 = (1, 3, 4)
t1 < t2

Output

False

Tuple assignment

#Tuple assignment

m = ('have', 'fun' , 'in', 'learning')
w, x, y, z = m
print (w,'\n',x,'\n',y,'\n',z)

Output

have
fun
in
learning

Another example

t = (1, 2, 3)
a, b = t

Output

ValueError: too many values to unpack (expected 2)

Dictionaries and tuples

Dictionary data structure has a method called items function. The items() functions return a list of tuples, where each tuple is a key-value pair:

d = {'a':10, 'b':1, 'c':22}
t = list(d.items())
print(t)

Output

[('a', 10), ('b', 1), ('c', 22)]

Multiple assignment with dictionaries

The dictionaries are traversed using for loop. In each iteration, the key is returned. we can combine items() function, tuple assignment, and for, to get both key and value pairs of dictionary in a single loop:

d = {'a':10, 'b':1, 'c':22}
for key in d:
    print (key, '-->', d[key])

Output

a --> 10
b --> 1
c --> 22
d = {'a':10, 'b':1, 'c':22}
for key, value in list(d.items()):
    print (key, '-->', value)

Output

a --> 10
b --> 1
c --> 22

Summary:

This tutorial discusses the basic concepts of tuple data structure in python. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds