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.
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.
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"
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.
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.
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.
By signing up, you agree to the Privacy Policy.
Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.