Course Outline (Part 12)

Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable (immutable). Tuples allow duplicate values.


1. Create tuple

Tuples are written with round brackets ().

thistuple = ("apple", "banana", "cherry")
print(thistuple)

Note on One-Item Tuples: To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple (it will treat it as a standard string/int in parentheses).

thistuple = ("apple",)
print(type(thistuple)) # <class 'tuple'>

# NOT a tuple
not_a_tuple = ("apple")
print(type(not_a_tuple)) # <class 'str'>

2. Access tuple items

You can access tuple items by referring to the index number, inside square brackets.

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])  # Outputs "banana"
print(thistuple[-1]) # Outputs "cherry"

3. Update tuple (convert to list)

Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.

However, there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi" # Change a value
y.append("orange") # Add a value
x = tuple(y)

print(x) # ('apple', 'kiwi', 'cherry', 'orange')

4. Unpack tuple

When we create a tuple, we normally assign values to it. This is called “packing” a tuple. But we are also allowed to extract the values back into variables. This is called “unpacking”.

fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)  # apple
print(yellow) # banana
print(red)    # cherry

Using Asterisk *

If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list.

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green) # apple
print(yellow) # banana
print(red)   # ['cherry', 'strawberry', 'raspberry']

5. Loop through tuple

You can loop through the tuple items by using a for loop.

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
    print(x)

6. Join tuples

To join two or more tuples you can use the + operator.

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)

If you want to multiply the content of a tuple a given number of times, you can use the * operator:

fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2

7. Tuple methods

Python has two built-in methods that you can use on tuples.

  • count(): Returns the number of times a specified value occurs in a tuple.

    thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
    x = thistuple.count(5) # Returns 2
  • index(): Searches the tuple for a specified value and returns the position of where it was found.

    thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
    x = thistuple.index(8) # Returns 3 (first occurrence)

Discussion

Loading comments...