Let's talk about lists in Python.
This is a list:
>>> colors = ["purple", "green", "blue", "yellow"]
We can prove that to ourselves by passing that object to Python's built-in type
function:
>>> type(colors)
<class 'list'>
Lists are ordered collections of things.
We can create a new list by using square brackets ([]
), and inside those square brackets, we put each of the items that we'd like our list to contain, separated by commas:
>>> numbers = [2, 1, 3, 4, 7, 11]
>>> numbers
[2, 1, 3, 4, 7, 11]
Lists can contain any type of object. Each item in a list doesn't need to be of the same type, but in practice, they typically are.
So we might refer to this as a list of strings:
>>> colors = ["purple", "green", "blue", "yellow"]
While this is a list of numbers:
>>> numbers = [2, 1, 3, 4, 7, 11]
We can check whether a list contains a particular item by using the in
operator.
The in
operator returns False
if the list doesn't contain that item, and it returns True
if it does:
>>> "red" in colors
False
>>> "green" in colors
True
We can get the number of items in a list by using the built-in len
function, which returns the length of our list:
>>> len(colors)
4
Here's an empty list, called languages
:
>>> languages = []
We can add an item to this empty list by using the list append
method:
>>> languages.append("Python")
The list append
method adds items to the end of a list:
>>> languages
['Python']
If there were already values in this list, we'd see the newly appended item appear after those values:
>>> languages.append("JavaScript")
>>> languages
['Python', 'JavaScript']
Lists are fine with duplicate items, so it doesn't matter if we're adding a value that's already in our list.
>>> languages.append("Python")
It will just appears a second time:
>>> languages
['Python', 'JavaScript', 'Python']
To remove an item from the end of a list, we can use the pop
method:
>>> languages.pop()
'Python'
>>> languages
['Python', 'JavaScript']
The pop
method both returns the item that it's removed, and also removes it from the list.
So we could point a variable to our pop
method call to store the item as we're removing it:
>>> removed_item = languages.pop()
>>> removed_item
'JavaScript'
>>> languages
['Python']
Aside: Changing an object in Python is often called mutating. The append and pop methods both mutate the list.
Lists are ordered, meaning they keep track of the relative positions of each item within them.
We can access individual items in a list based on their position by indexing the list:
>>> colors = ["purple", "green", "blue", "yellow"]
>>> colors[1]
'green'
Indexing uses square brackets at the end of the variable name that points to our list. This is sometimes called subscript notation.
Lists use zero-based indexes, so the first item has an index of 0
:
>>> colors[0]
'purple'
We can change which item is at a particular index by assigning to that index:
>>> colors[0] = "pink"
>>> colors
['pink', 'green', 'blue', 'yellow']
Lists also support negative indexing.
Index -1
gives us the last item in a list,-2
gives the second to last, and so on:
>>> colors[-1]
'yellow'
>>> colors[-2]
'blue'
A data structure (a.k.a. a collection) is an object that keeps track of other objects. Lists are the most commonly used data structure in Python.
Anytime you need to store and manipulate an ordered collection of things, you should consider using a list.
We don't learn by reading or watching. We learn by doing. That means writing Python code.
Practice this topic by working on these related Python exercises.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!