In This post we will going to talk about another type of Python data type called tuples, it is quit same as list but on other side has huge difference. So Tuples are also sequence data types and unlike list we can’t change them, this make quit suitable for many reasons. It also quit fast to process tuples as compared list.

With below examples we could understand, tuples is quit fast as compared to list, sure in smaller examples or process we never need to care for these things but we should be aware of real power of tools we used, god knows when we need to use it for some useful task.

Creation of Tuples

Tuples could be created like in below code

>>> tup=("a","b","c")
>>> type(tup)
<type 'tuple'>

So you have to mention your tuple elements in brackets separated through comma like above

We can also create empty like below

>>> tup=()
>>> type(tup)
<type 'tuple'>
>>> len(tup)

>>> tup1=tuple()
>>> len(tup1)
>>> type(tup1)
<type 'tuple'>

What if we need only one element in tuple, we need to assign like below.

>>> tuple=(1,)
>>> type(tuple)
<type 'tuple'>
>>> tuple

There we should add trailing comma after your item.

But there is another way to create tuple which might not suggest in theory like below.

>>> tup1=1,2,3,4,5,6
>>> type(tup1)
<type 'tuple'>
>>> tup1
(1, 2, 3, 4, 5, 6)

Access tuples elements

we could also access tuples elements through indexing like below. It is same as list. Indexing is zero based will not include second index.

>>> tup[0]
>>> tup[0:1]
>>> tup[0:2]
('a', 'b')

Tuple in Loop

Tuple could use in loop as we could use use any array or Python list, see one below code for same.

>>> tup1=("C","C++","C#","Java","Bash","python","PHP","Golang")
>>> for i in tup1:
...     print i

We have another examples which use enumerate function and work like below, it also contain count of every iteration.

>>> tup2=("Sang","Suk","Ishu")
>>> for index, person in enumerate(tup2):
...     print index, person
0 Sang
1 Suk
2 Ishu

Not many methods are associates with tuples in python like len, index, count etc. As said we can’t change tuples after initialise so Immutable and list that could change , update anytime are mutable

Example of how we could understand we can’t change tuples after initialise is like below.

>>> tup1
('C', 'C++', 'C#', 'Java', 'Bash', 'python', 'PHP', 'Golang')
>>> tup1[0]="Autocord"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

We can’t change it, but we can join two or use a slice of tuples to make another tuple anytime

>>> tup1
('C', 'C++', 'C#', 'Java', 'Bash', 'python', 'PHP', 'Golang')
>>> tup2
('Sang', 'Suk', 'Ishu')
>>> tup3=tup1+tup2
>>> tup3
('C', 'C++', 'C#', 'Java', 'Bash', 'python', 'PHP', 'Golang', 'Sang', 'Suk', 'Ishu')

Same we could use to unpack or pack tuple like we can do in list, For examples just tried to make it in below code.

>>> a,b=(1,2)
>>> a
>>> b

Benefits of Tuples over list

Although we can’t do much with tuples as we can’t add or delete any new element from tuples once it get created. But as said already there are some benefits of using tuples like one mentioned above, tuples are way faster than list like we tried to calculate in below code.

>>> timeit.timeit('x=[1,2,3,4,5,6,7,8,9,10,11,12]', number=9999999)
>>> timeit.timeit('x=(1,2,3,4,5,6,7,8,9,10,11,12)', number=9999999)

Tuple could also used as dictionary keys because they are immutable and list should never used as such because they could change within program anytime. Same tuples could also use values in set but list can’t.

So tuples play an important rules in Python data coding as array which is immutable, fast and reliable to use for some static values.