Sequences in Python

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video
02:03

Sequences are a special type of iterable that can be indexed using square brackets ([...]) to get items by their position. You can also ask sequences for their length to see how many things are inside them.

A sequence is an ordered collection. They maintain the order of the things in them.

Which ones are sequences?

Here we have a list, a tuple, a string, a set, and a dictionary:

>>> fruits = ['apple', 'lemon', 'pear', 'watermelon']
>>> coordinates = (1, 8, 2)
>>> greeting = "Hi y'all!"
>>> colors = {'red', 'blue', 'yellow'}
>>> item_counts = {'computers': 1, 'headphones': 2, 'ducks': 3}

We can write a for loop to loop over these (they're all iterables).

>>> for fruit in fruits:
...     print(fruit)
...
apple
lemon
pear
watermelon

And we can get the length of any of these (using the built-in len function).

>>> len(fruits)
4

But not all of these are indexable.

All of these are iterables, but not all of them are sequences. Only the list, tuple, and string above are sequences.

The properties of a sequence

Sequences can be indexed from 0 up until index len(my_sequence)-1.

Index 0 represents the first item in a sequence:

>>> fruits[0]
'apple'

Sequences can also usually be negative indexed to get items from the end of the sequence.

Index -1 represents the last thing in a sequence:

>>> coordinates[-1]
2

You can usually slice sequences also. This will give us everything up to (but not including) the last character in the greeting string (so we get everything but the exclamation mark):

>>> greeting[:-1]
"Hi y'all"

Iterables that aren't sequences

Sets are iterables, but they're not sequences:

>>> colors[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not subscriptable

So, if we try to index a set, it's not going to work.

If we try to index a dictionary, it might seem like it works depending on what the keys the dictionary are.

>>> item_counts[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 0

But we're not actually indexing the dictionary here, we're doing a key-value lookup.

If you try to slice a dictionary as well:

>>> item_counts[:-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'slice'

Lists, tuples and strings are sequences but sets and dictionaries are not.

Summary

Sequences are iterables that have a length and can be indexed.

You can usually slice sequences. You can also usually negative index them.

The most common sequences built-in to Python are strings, tuples, and lists (though range objects are also sequences, which is interesting). You'll also see other sequences floating around in Python, but strings, tuples, and lists are the most common ones.

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.

Python Morsels
Watch as video
02:03