Press "Enter" to skip to content

Python Tuple

Zigya Acadmey 0

Tuples are an ordered sequences of items, just like lists. The main difference between tuples and lists is that tuples cannot be changed (immutable) unlike lists which can (mutable).

# Way 1
emptyTuple = ()

You can also initialize an empty tuple by using the tuple function.

# Way 2
emptyTuple = tuple()

A tuple with values can be initialized by making a sequence of values separated by commas.

# way 1
z = (3, 7, 4, 2)
# way 2 (tuples can also can be created without parenthesis)
z = 3, 7, 4, 2

It is important to keep in mind that if you want to create a tuple containing only one value, you need a trailing comma after your item.

# tuple with one value
>>> tup1 = ('Michael',)
# tuple with one value
>>> tup2 = 'Michael',
# This is a string, NOT a tuple.
>>> notTuple = ('Michael')

Accessing Values in Tuples

Each value in a tuple has an assigned index value. It is important to note that python is a zero indexed-based language. All this means is that the first value in the tuple is at index 0.

# Initialize a tuple
>>> z = (3, 7, 4, 2)
# Access the first item of a tuple at index 0
>>> print(z[0])
3

Python also supports negative indexing. Negative indexing starts from the end of the tuple. It can sometimes be more convenient to use negative indexing to get the last item in a tuple because you don’t have to know the length of a tuple to access the last item.

# print last item in the tuple
>>> print(z[-1])
2

Tuple slices

Slice operations return a new tuple containing the requested items. Slices are good for getting a subset of values in your tuple. For the example code below, it will return a tuple with the items from index 0 up to and not including index 2.

# Initialize a tuple
>>> z = (3, 7, 4, 2)
# first index is inclusive (before the :) and last (after the :) is not.
>>> print(z[0:2])
3, 7
# everything up to but not including index 3
>>> print(z[:3])
3, 7, 4

Tuples are Immutable

Tuples are immutable which means that after initializing a tuple, it is impossible to update individual items in a tuple. As you can see in the code below, you cannot update or change the values of tuple items (this is different from Python List which are mutable).

>>> z = (3, 7, 4, 2)
>>> z
(3, 7, 4, 2)
>>> z[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Even though tuples are immutable, it is possible to take portions of existing tuples to create new tuples as the following example demonstrates.

# Initialize tuple
tup1 = ('Python', 'SQL')
# Initialize another Tuple
tup2 = ('R',)
# Create new tuple based on existing tuples
new_tuple = tup1 + tup2;
print(new_tuple)

Iterate through a Tuple

You can iterate through the items of a tuple by using a for loop.

>>> for item in ('lama', 'sheep', 'lama', 48):
>>>   print(item)
lama
sheep
lama
48

Enumerate

The enumerate function returns a tuple containing a count for every iteration (from start which defaults to 0) and the values obtained from iterating over a sequence:

>>> friends = ('Steve', 'Rachel', 'Michael', 'Monica')
>>> for index, friend in enumerate(friends):
>>>    print(index,friend)
0 Steve
1 Rachel
2 Michael
3 Monica

Conclusion

Hence we studied in this article what is tuple in python and how to implement along with its different properties and how to iterate tuple.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

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