meta data for this page
  •  

tuples

Store multiple items in a single variable. Tuple:

  • ordered (has index)
  • allow duplicated (index is unique)
  • immutable
  • can contain different data types
# create tuple
a = ( 1, "a", True )
 
# create tuple with one item NOTE: comma after item
a = ( 1, )

"Change" items in tuple

Tuples are immutable. Only way is to create new tuple using temporary list.

t = ('a', 'b', 'c')
t_new = t + ('d', 'e', 'f')
 
l = list(t)
 
l.insert('d', 'e', 'f')
l[1] = 'bb'
l.remove(0)
 
t_new = tuple(l)