02-Tuples

1. Overview

Important
  • A Tuple is rather similar to a list, but is defined using round brackets, ().
  • Tuples can contain multiple elements, and the elements may not share the same type. The key difference is that tuples are immutable, so that once a tuple is created it cannot subsequently be changed. (It can be extended - but this is effectively the creation of a new, larger tuple!).
  • As a lists, the indivisual elements of a tuple can be accessed using [index] after the tuple name.
Shell
>>> a = (1, 2, 3, 'x', (1, 2, 3), [5, 6, 7])
>>> print(a)
(1, 2, 3, 'x', (1, 2, 3), [5, 6, 7])
>>> type(a)
<class 'tuple'>
>>> 

2. Convert to list and vice versa

Note

A tuple can be converted into a list and vice versa

Shell
>>> a = [3, 4, 5]
>>> type(a)
<class 'list'>
>>> b = (6, 7, 8)
>>> type(b)
<class 'tuple'>
>>> 
>>> ta = tuple(a)
>>> print(ta)
(3, 4, 5)
>>> type(ta)
<class 'tuple'>
>>> 
>>> lb = list(b)
>>> print(b)
(6, 7, 8)
>>> print(lb)
[6, 7, 8]
>>> type(lb)
<class 'list'>