Tuple
what are tuples? how to create them? when to use them? and tuple methods

What are tuples?

In Python, tuples are similar to lists except tuples are immutable. While lists are mutable and you can change/modify elements in a tuple after the tuple is created, but you cannot change the elements of a tuple once the tuple is created.

Tuples vs Lists

  1. 1.
    Elements of a tuple are enclosed in parenthesis () whereas the elements of list are enclosed in square bracket [].
  2. 2.
    The elements of a list are mutable whereas the elements of a tuple are immutable.
  3. 3.
    It is recommended to use tuple data type when you do not modify data and use list data type when you plan to update/modify/change data in the near future.
  4. 4.
    Iterating over the elements of a tuple is faster compared to iterating over a list.

When to use tuples?

It is good to use tuples when
  1. 1.
    you are not changing your data over time.
  2. 2.
    There's a strong culture of tuples being for heterogeneous collections.
  3. 3.
    Iterating over the elements of a tuple is fast so it is recommend to be used when doing iterations of immutable data collections.
  4. 4.
    Python dictionary requires tuple as one of its components, a value that is of an immutable type. A tuple can be used for this purpose, whereas a list can’t be.

How to create tuples?

You can create a tuple by placing all the elements inside parentheses (), separated by commas ,.
Normally, parentheses are NOT necessary and a tuple can be created as a comma separated elements. However, it is a good practice to use parentheses.
Similar to a list, a tuple elements can contain different data types (string, integer, float, list, another tuple, etc.).

Create tuple with ()

Code
Output
1
# An empty tuple
2
my_tuple = ()
3
print(my_tuple)
Copied!
1
# Output:
2
()
Copied!
A tuple contain integer
Code
Output
1
my_tuple = (1, 2, 3)
2
print(my_tuple)
Copied!
1
# Output:
2
(1, 2, 3)
Copied!
A tuple with strings
Code
Output
1
my_tuple = ('Krakow', 'Warsaw', 'Poznan', 'Wroclaw')
2
​
3
print(my_tuple)
Copied!
1
# Output
2
('Krakow', 'Warsaw', 'Poznan', 'Wroclaw')
Copied!
Tuple with mixed datatypes
Code
Output
1
my_tuple = (3, 10, 3.14, 'Krakow', True)
2
​
3
print(my_tuple)
Copied!
1
(3, 10, 3.14, 'Krakow', True)
Copied!
Nested tuple
Code
Output
1
my_tuple = ('Krakow', ['zone 1', 'zone 2'], 'Warsaw', (1, 2, 3, 4))
2
​
3
print(my_tuple)
Copied!
1
('Krakow', ['zone 1', 'zone 2'], 'Warsaw', (1, 2, 3, 4))
Copied!

Create tuple without ()

A tuple can also be created without using parentheses (). This is called tuple packing.
Code
Output
1
my_city_tuple = 'Krakow', 'Warsaw', 'Poznan', 'Wroclaw'
2
​
3
print(my_city_tuple)
Copied!
1
('Krakow', 'Warsaw', 'Poznan', 'Wroclaw')
Copied!

Tuple unpacking

Each element of a tuple can be access and assigned to a new variable.
Code
Output
1
city1, city2, city3, city4 = ('Krakow', 'Warsaw', 'Poznan', 'Wroclaw')
2
​
3
print(city1)
4
print(city2)
5
print(city3)
6
print(city4)
Copied!
1
# Output
2
Krakow
3
Warsaw
4
Poznan
5
Wroclaw
Copied!

A single element tuple

It is a bit tricky to create a single element tuple. We cannot easily wrap an element inside a parenthesis (). This easily worked for a single element list.
Code
Output
1
# string
2
my_tuple = ('students')
3
​
4
print(my_tuple)
5
print(type(my_tuple))
6
​
7
# integer
8
my_number_tuple = (24)
9
​
10
print(my_number_tuple)
11
print(type(my_number_tuple))
Copied!
1
# print(my_tuple)
2
'students'
3
​
4
# print(type(my_tuple))
5
<class 'str'>
6
​
7
​
8
# print(my_number_tuple)
9
24
10
​
11
# print(type(my_number_tuple))
12
<class 'int'>
Copied!
You need to convert from simple datatype to complex datatype of tuple, you have to add comma , at the end.
Code
Output
1
# string
2
my_tuple = ('students', )
3
​
4
print(my_tuple)
5
print(type(my_tuple))
6
​
7
# integer
8
my_number_tuple = (24, )
9
​
10
print(my_number_tuple)
11
print(type(my_number_tuple))
Copied!
1
# print(my_tuple)
2
'students'
3
​
4
# print(type(my_tuple))
5
<class 'tuple'>
6
​
7
​
8
# print(my_number_tuple)
9
24
10
​
11
# print(type(my_number_tuple))
12
<class 'tuple'>
Copied!

Access Tuple Elements

Similar to lists, you can use index operator [] to access an element in a tuple. Index always starts from ZERO in Python. You can perform all the slicing operations exactly as lists except tuple cannot be modified (immutable)
indexing code
indexing output
Negative indexing code
Negative indexing output
1
my_tuple = (1, 2, 3, 4, 5, 6, 7)
2
​
3
my_tuple[2]
Copied!
1
# Output:
2
3
Copied!
1
my_tuple = (1, 2, 3, 4, 5, 6, 7)
2
​
3
my_tuple[-3]
Copied!
1
# Output
2
4
Copied!
You can use slicing operations on tuples similar to lists.
Code
Output
1
my_tuple_cities = ('Krakow', 'Warsaw', 'Kielce', 'Katowice', 'Wroclaw')
2
​
3
my_tuple_cities[1:-1]
Copied!
1
('Warsaw', 'Kielce', 'Katowice')
Copied!
You can also access nested list
Code
Output
1
my_tuple_cities = ('Krakow', 'Warsaw', [1, 2, 3], (2.1, 2.2, 2.3), 'Wroclaw')
2
​
3
my_tuple_cities[2][2]
Copied!
1
# Output
2
3 # index 2 of the tuple and index 2 of the list
Copied!
and nested tuple as well.
Code
Output
1
my_tuple_cities = ('Krakow', 'Warsaw', [1, 2, 3], (2.1, 2.2, 2.3), 'Wroclaw')
2
​
3
my_tuple_cities[3][1]
Copied!
1
# Output
2
2.2 # index 3 of the tuple and index 1 of the nested tuple
Copied!
You can do concatenation of two tuples with + operator.
Code
Ouput
1
tuple_1 = (2, 4, 6)
2
tuple_2 = (1, 3, 5)
3
​
4
tuple_1 + tuple_2
Copied!
1
# Output
2
(2, 4, 6, 1, 3, 5)
Copied!
You can also multiple a tuple multiple times with * operator.
Code
Output
1
animals = ('cat', 'dog')
2
​
3
animals * 2
Copied!
1
# Output
2
('cat', 'dog', 'cat', 'dog')
Copied!
You can check the Jupyter Notebook in GitHub or Colab.
Last modified 1yr ago