July 1, 2021

Every Python Tuple Method Explained Simply (plus bonus tidbits)

Every Python Tuple Method Explained Simply (plus bonus tidbits)

A tuple in Python is a data structure that is similar to a list but it is unchangeable. This means that it is immutable by design. Lists, in contrast, are highly mutable. If you try and make changes to an immutable object, it will throw an error.

Here is a quick summary of what a tuple is:

  • written with ()
  • unchangeable, ordered, indexed
  • duplicates allowed
  • looks something like this:
pythontuple = ('python','javascript','learn')

Now let’s get right into the methods available for tuples in Python.

tuple.count()

count() will count the number of times a value appears in a tuple. For example:

word = ('h','e','l','l','o','!')
print(word.count('l'))

# this will return:
# 2

As you can have nested tuples, you can also count complex values. For example:

numbers = ([1,2],[1,2],[3,4])
print(numbers.count([1,2]))

# this will return:
# 2

tuple.index()

Use index() if you want to find out the index of a particular value. It is good to note that index() will first the first matched value and will not iterate through the entire list. For example:

word = ('h','e','l','l','o','!')
print(word.index('l'))

# this will return:
# 2

And that’s basically all the methods available for tuples. On the surface, it looks like you can’t do much with tuples. This is in part because a tuple is immutable (you cannot change it). However, you can still do quite a lot with tuples in general. Here is a quick run-down on the different techniques you can use for cases that might crop up for your project.

You can change a list inside a tuple

Tuples are immutable — however, they can contain lists, which are mutable. For example, this is a valid tuple:

sometuple = (0, ['a'])

# to access the value ['a'] and change it
sometuple[1][0] = 'b'
print(sometuple)

# this will return:
# (0, ['b'])

However, you cannot change the type within the tuple. For example, you can’t change a list to a single variable. This will throw an item assignment error. For example, you cannot do this:

sometuple = (0, ['a'])
sometuple[1] = 'b'print(sometuple)

# this will return:
# Traceback (most recent call last):
#  File "<pyshell#6>", line 1, in <module>
#    sometuple[1] = 'b'
# TypeError: 'tuple' object does not support item assignment

Tuples with 0 or only 1 items

A tuple with only 1 item will be treated as a string. For a tuple to be a tuple, you need a dataset. To get around having only 1 item, you can add a comma to force it into a tuple. For example:

sometuple = ('a',)

Empty tuples are still treated as tuples.

Accessing tuples by index or slice

To access data inside a tuple, use either an integer or a slice. For example:

word = ('h','e','l','l','o','!')

# accessing via index 
integerprint(word[2])

# this will return:
# l

# accessing via slice
print(word[2:5])

# this will return:
# ('l', 'l', 'o')

And that’s basically it for tuples.