CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – Tuples – A walk through with Examples

Another sequence type, Python provides is Tuple. Unlike Lists, Tuples are immutable; that means, Tuples doesn’t allow to modify the data.

In this article, I am going to explain, how to create & access data from Tuples.

Create a tuple in Python

We can create tuple in Python using comma “,” or parentheses (“()”) or using tuple() constructor. Each element in tuple is separated by a comma (“,”). We can create an empty tuple with parentheses (“()”) or by calling zero argument constructor, tuple().

Below is the example to create a tuple:

>>> tpl = (1, 2, 3, "Apple", 5.0)
>>> tpl
(1, 2, 3, 'Apple', 5.0)
>>>

We can also create a tuple using below statement; parentheses is optional, but it is mandatory while creating an empty tuple, OR we can use tuple() constructor to create an empty tuple:

>>> tpl = 1, 2, 3, "Apple", 5.0

Accessing tuple elements

Like Lists, we can access the elements in tuple, using its’ positions; the position will start from “0” and the last element position is (number of elements in tuple – 1).

Negative index positions also we can use to access tuple’s elements. Negative index position work from the end of the tuple; starts from “-1” position.

For example, see, below statements to access first element & last element from tuple.

>>> tpl[0]
1
>>> tpl[-1]
5.0
>>>

Another example, we use to access its’ elements using for statement:

>>> for element in tpl:
... print(element)
...
>>>

Tuple within a Tuple

Python allows to add tuples inside the tuples.  To access inside tuple’s elements, we need to use multi index; called multi dimensional. For example, below example access inside tuple’s elements:

>>> tpl = tuple((1, 2, 3, "Apple", 5.0, (1.1, 2.2, 3.3, "Banana", 5.5)))
>>> tpl
(1, 2, 3, 'Apple', 5.0, (1.1, 2.2, 3.3, 'Banana', 5.5))
>>> tpl[0]
1
>>> tpl[-1]
(1.1, 2.2, 3.3, 'Banana', 5.5)
>>> tpl[-1][0]
1.1
>>> tpl[-1][-1]
5.5
>>>

Notice that when we create a tuple using tuple() constructor; we must pass the tuple elements inside the parentheses “()”; above, the one marked in blue color. Otherwise, we will get below error message:

>>> tpl = tuple(1, 2, 3, 'Apple', 5.0, (1.1, 2.2, 3.3, 'Banana', 5.5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: tuple() takes at most 1 argument (6 given)

List within a Tuple

Even, Python allows to include Lists within Tuples. And interestingly, we can access or modify list elements that are inside the tuple; but we can’t modify tuple elements. We can’t modify the list itself with in the tuple; but we can modify list elements. Below example, explains this:

>>> tpl = tuple((1, 2, 3, "Apple", 5.0, [100, 200, 300, "Banana", 7.0]))
>>> tpl
(1, 2, 3, 'Apple', 5.0, [100, 200, 300, 'Banana', 7.0])
>>> tpl[-1]
[100, 200, 300, 'Banana', 7.0]
>>> tpl[-1][-1] = 9.0
>>> tpl
(1, 2, 3, 'Apple', 5.0, [100, 200, 300, 'Banana', 9.0])
>>>

We will discuss more about Python in upcoming articles.

/Shijit/

Python – Tuples – A walk through with Examples

Leave a Reply

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

Scroll to top