• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Python | Tuples
A Tuple is an immutable collection of ordered items.
Items in the tuple can be of any type, including mixed types within the same tuple: strings, numbers, dictionaries, lists, sets, objects, tuples, ...
Tuples allow duplicates.
Items of tuples can be accessed by position (0-based indexing).

Creating basic lists:
Use parentheses (()) to create a tuple.
Use commas to separate items in the tuple.
x = (0, 1, 2, 3)
print(type(x)) # Output: <class 'tuple'>

print(x) # Output: (0, 1, 2, 3)
print(x[1]) # Output: 1
Tuples with one item:
# must use the comma after the single element of the tuple
y = (0,)
print(y) # Output: (0,)

# alternative syntax for single-item tuple
y = 0,
print(y) # Output: (0,)

# missing the comma will create an object that is not a tuple
not_a_tuple = (10)
print(not_a_tuple) # Output: 10
print(type(not_a_tuple)) # Output: <class 'int'>
Empty tuples:
y = ()
z = tuple()
print(f'{y} - {len(y)}') # Output: () - 0
print(f'{z} - {len(z)}') # Output: () - 0
Nested tuples:
y = (0,)
z = (x, y, 'a')
print(z) # Output: ((0, 1, 2, 3), (0,), 'a')
Immutability:
If you try to modify a tuple you get an error:
# Raises TypeError: 'tuple' object does not support item assignment
x[0] = 3
Tuples packing and unpacking:
# packing
x = 1, 2, 3  # equivalent to x = (1, 2, 3)
print(x) # Output: (1, 2, 3)

# unpacking
a, b, c = x
print(a) # Output: 1
print(b) # Output: 2
print(c) # Output: 3
Indexing:
Accessing elements using square brackets with zero-based indexing:
x = ('A', 'B', 'C', 'D')
print(x[0]) # Output: A
print(x[1]) # Output: B
print(x[-1]) # Output: D
print(x[-2]) # Output: C
Slicing:
Extract parts of a tuple:
x = (0, 1, 2, 3, 4, 5)

# [i:j]: slice staring from index i, until j excluded
print(x[2:4]) # Output: (2, 3)

# [:i]: slice up to index i excluded
print(x[:2]) # Output: (0, 1)

# [i:]: slice staring from index i included, until last element of the tuple
print(x[3:]) # Output: (3, 4, 5)

# [::i]: slice staring from index 0, including every second element
print(x[::2]) # Output: (0, 2, 4)

# [::-1]: tuple with elements reversed
print(x[::-1]) # Output: (5, 4, 3, 2, 1, 0)
Tuple built-in methods:
  • Common operations:
    # count(ITEM) - returns number of occurences
    print(('A', 'A', 'B').count('A')) # Output: 2
    
    # length
    print(len(('A', 'B'))) # Output: 2
    
    # Min/Max/Sum (comparable elements)
    print(min((1, 2, 3))) # Output: 1
    print(max((1, 2, 3))) # Output: 3
    print(sum((1, 2, 3))) # Output: 6
    
    # Concatenation
    print(('A', 'B') + ('B', 'C')) # Output: ('A', 'B', 'B', 'C')
    
    # Repetition
    print(('A', 'B') * 2) # Output: ('A', 'B', 'A', 'B')
    
    # Membership
    print('A' in ('A', 'B')) # Output: True
    print('A' not in ('A', 'B')) # Output: False
  • Finding elements:
    # index(ITEM) - returns first index of item
    print(('A', 'A', 'B').index('B')) # Output: 2
    
    # index(ITEM, START_INDEX) - returns first index of item starting from START_INDEX
    print(('A', 'A', 'B').index('A', 1)) # Output: 1
    
    # index(ITEM, START_INDEX, END_INDEX) - returns first index of item between START_INDEX and END_INDEX
    print(('A', 'A', 'B').index('A', 1, 2)) # Output: 1
  • Creating tuples:
    # from string
    print(tuple('AB')) # Output: ('A', 'B')
    
    # from range
    print(tuple(range(2))) # Output: (0, 1)
    
    # from list
    print(tuple(['A', 'B'])) # Output: ('A', 'B')
    
    # from set (reminder that sets are unordered)
    print(tuple({'A', 'B'})) # Output: ('B', 'A')
  • Converting tuples:
    # to list
    print(list(('A', 'B'))) # Output: ['A', 'B']
    
    # from set (reminder that sets are unordered)
    print(set(('A', 'B'))) # Output: {'B', 'A'}
© 2025  mtitek