Tuple in Python With examples

Spread the love

Tuple –

What is a Tuple in Python ?

A tuple is just like a List but the main difference between tuple and list is that Tuple is immutable means it can not be changed once you created it compared to Lists which you can change. A tuple is an immutable List. Another difference is that to represent tuple in python we use parenthesis compared to list which uses Square Brackets.

How to Create a Tuple in Python?

To create a tuple we use parenthesis and put every elements inside this parenthesis separated by commas.

In [1]: # create an empty tuple

In [2]: empty_tup = ()

In [3]: type(empty_tup)
Out[3]: tuple

In [4]: # create a tuple with integers

In [5]: int_tup = (1, 2, 3, 4, 5)

In [6]: type(int_tup)
Out[6]: tuple

In [7]: # a tuple with mix data type

In [8]: mix_tup = (1, 'hello', 2.4,[5, 6, 7])

In [9]: type(mix_tup)
Out[9]: tuple

In [10]: 

To create a tuple with only one element, you have to add a comma after the element.

In [10]: not_tup = (5)

In [11]: type(not_tup)
Out[11]: int

In [12]: good_tup = (5,)

In [13]: type(good_tup)
Out[13]: tuple

In [14]: 

Accessing Elements of a Tuple –

There are various way to access elements of a tuple. Let’s look at them one by one.

1 . Indexing –

To access elements from a tuple we use the square bracket followed by the index or position of the item in the tuple. Indexing in python always starts with 0 not 1. For example if you want to get the first item of a tuple you have to write tuple[0] not tuple[1].

In [14]: companies = ('Google','Apple','Microsoft','Facebook')

In [15]: # access for element 

In [16]: companies[0]
Out[16]: 'Google'

In [17]: # access the second element

In [18]: companies[1]
Out[18]: 'Apple'

In [19]: 

Nested tuples can be accessed like this.

In [19]: num_tup = (1, 2, 7, [10, 12])

In [20]: # get 12 from the tuple

In [21]: num_tup[3][1]
Out[21]: 12

In [22]: 

2 . Negative Indexing –

You can also use Negative Indexing to access tuples in python. Negative indexing starts from -1 from the end of a tuple. To get the last item using negative indexing you have to write tuple[-1] to get the second last item tuple[-2].

In [22]: companies
Out[22]: ('Google', 'Apple', 'Microsoft', 'Facebook')

In [23]: companies[-1]
Out[23]: 'Facebook'

In [24]: companies[-2]
Out[24]: 'Microsoft'

In [25]: 

3 . Slicing –

You can also access multiple items from a tuple using slicing in python.

Syntax for slicing –


start – index from which you want to start

stop – index before which you want to stop

step – the step size. By default it is 1.

When you do slicing, start is included and end is excluded. Let’s say you want to get the middle two items Apple and Microsoft.

In [25]: companies
Out[25]: ('Google', 'Apple', 'Microsoft', 'Facebook')

In [26]: # get Apple and Microsoft

In [27]: companies[1:3]
Out[27]: ('Apple', 'Microsoft')

In [28]: 

If you want you can also omit any start or end index or both. The following code tells python to get everything from the start and stop before the 2nd index.

In [28]: companies[:2]
Out[28]: ('Google', 'Apple')

In [29]: 

And the following code say start from the 2nd index and go till the end.

In [29]: companies[2:]
Out[29]: ('Microsoft', 'Facebook')

In [30]: 

Omitting both the index means gives me everything from start to end.

In [30]: companies[:]
Out[30]: ('Google', 'Apple', 'Microsoft', 'Facebook')

In [31]: 

To get every other element we can use the step size.

In [32]: companies
Out[32]: ('Google', 'Apple', 'Microsoft', 'Facebook')

In [33]: companies[::2]
Out[33]: ('Google', 'Microsoft')

In [34]: 

Immutability –

As we discussed before that you can not change a tuple once it is created.

In [34]: num_tup
Out[34]: (1, 2, 7, [10, 12])

In [35]: num_tup[0] = 10
Traceback (most recent call last):

  File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_27112\2085553011.py", line 1, in <module>
    num_tup[0] = 100

TypeError: 'tuple' object does not support item assignment

In [36]: 

But if the element itself is mutable like a list then you can change the element of the list.

In [36]: num_tup
Out[36]: (1, 2, 7, [10, 12])

In [37]: num_tup[3][0] = 100

In [38]: num_tup
Out[38]: (1, 2, 7, [100, 12])

In [39]: 

Rating: 1 out of 5.

Leave a Reply