Introduction

Tuple is similar to a List. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas, in a list, elements can be changed.

Creating a Tuple

A tuple is created by placing all the items (elements) inside parentheses (), separated by commas. A tuple can have any number of items and they may be of different types (integer, float, list, string, etc).

# Empty tuple
my_tuple = ()

# Output: ()
print(my_tuple)  

# Tuple having integers
my_tuple = (1, 2, 3)

# Output: (1, 2, 3) 
print(my_tuple)  

# Tuple with mixed data types
my_tuple = (1, "Hello", 3.4)

# Output: (1, "Hello", 3.4)
print(my_tuple)    

# Nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# Output: ("mouse", [8, 4, 6], (1, 2, 3)) 
print(my_tuple)

When you try to define a tuple with one item, Since parentheses are also used to define operator precedence in expressions, Python evaluates the expression (2) as simply the integer 2 and creates an int object. To tell Python that you really want to define a singleton tuple, include a trailing comma (,) as shown below

t = (2)

# Output: <class 'int'>
type(t)

t = (2,)

# Output: <class 'tuple'>
type(t)

Accessing Elements

We can use the index operator [] to access an item in a tuple where the index starts from 0. Likewise, nested tuples are accessed using nested indexing. Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a tuple by using the slicing operator.

my_tuple = ('p','e','r','m','i','t')

# Output: ('e', 'r', 'm')
print(my_tuple[1:4])

# Output: ('p', 'e')
print(my_tuple[:-4])

# Output: ('i', 't')
print(my_tuple[4:])

# Output: ('p', 'e', 'r', 'm', 'i', 't')
print(my_tuple[:])

Modifying Tuple

Unlike lists, tuples are immutable. This means that elements of a tuple cannot be changed once it has been assigned. But, if the element is itself a mutable datatype like list, its nested items can be changed. We can use + operator to combine two tuples. We can also repeat the elements in a tuple for a given number of times using the * operator. Both + and * operations result in a new tuple.

my_tuple = (4, 2, 3, [6, 5])

# TypeError: 'tuple' object does not support item assignment
# my_tuple[1] = 9

# Mutable element can be changed
my_tuple[3][0] = 9

# Output: (4, 2, 3, [9, 5])
print(my_tuple)

# Tuples can be reassigned
my_tuple = ('p','r','o')

# Output: ('p', 'r', 'o')
print(my_tuple)

# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))

# Repeat
# Output: ('Repeat', 'Repeat', 'Repeat')
print(("Repeat",) * 3)

Deleting Tuple

As discussed above, we cannot change the elements in a tuple. That also means we cannot delete or remove items from a tuple. But deleting a tuple entirely is possible using the keyword del.

my_tuple = ('p','r','o')

# Can't delete items
# TypeError: 'tuple' object doesn't support item deletion
# del my_tuple[2]

# Can delete an entire tuple
del my_tuple

Important Methods

Methods that add items or remove items are not available with tuple. Only the following two methods are available.

  • count(x) – Returns the number of items x
  • index(x) – Returns the index of the first item that is equal to x

Advantages of Tuple

Advantages of  a tuple over a list

  • Since tuples are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
  • Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not possible.
  • If you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected.