Lists
A list is a data type that holds a sequence of related pieces of data. Lists are also known as arrays in many programming languages.
The shortest list is the empty list, which we can construct with two square brackets:
members = []
An empty list doesn't seem very useful at first, but we'll soon learn how to add more items to empty lists.
To construct lists with data inside, put the values between the brackets and separate the values by commas:
members = ["Pamela", "Tinu", "Brenda", "Kaya"]
ages_of_kids = [1, 2, 7]
prices = [79.99, 49.99, 89.99]
As you can see from those examples, lists can hold any Python data type. In fact, Python even lets you mix and match data types:
remixed = ["Pamela", 7, 79.99]
I wouldn't recommend that, personally, since a list should hold sequences of related data, and it's unclear how those items form a sequence or how they are related. But there may be some situations where it's helpful.
Accessing list items
It's possible to reference a specific item in a list by using bracket notation: the name of the variable, followed by square brackets with the item index inside.
list_name[index]
The index of items in a list starts at 0:
temps = [48.0, 30.5, 20.2, 99.0, 52.0]
# Index: 0 1 2 3 4
That means that the final item in a 5-item list has an index of 4, not 5. Here's how we would access each of the items in that list:
temps[0] # 48.0
temps[1] # 30.5
temps[2] # 20.2
temps[3] # 99.0
temps[4] # 52.0
There is no item at index 5, so running temps[5]
would result in the error "IndexError: list index out of range". The zero-indexing scheme is often a source of confusion, especially since there are a few languages that start list items at index 1, so this is another common source of off-by-one errors. 😱
Python also allows for negative indexing, which goes backwards from the end of the list:
temps = [48.0, 30.5, 20.2, 99.0, 52.0]
# Index: -5 -4 -3 -2 -1
Using the indices looks like this:
temps[-1] # 52.0
temps[-2] # 99.0
Once again, going too far will result in an error. In this case, since the negative indices start from -1 (not -0, since apparently negative zero is not a thing!), the first out-of-bounds index is temps[-6]
.
Self-check: Favorite singers
Consider this list of my favorite musical artists:
fav_singers = ["Robyn", "Ed Sheeran", "Pink", "Lil Nas X", "Janelle Monáe", "Ben Platt"]
List length
Python provides the global function len()
to find out the length of a list (or any sequence).
attendees = ["Tammy", "Shonda", "Tina"]
len(attendees) # Evaluates to: 3
The length of a list is how many items are in the list, which is different from the maximum index in a list. A list with a len()
of 3 will have a maximum index of 2.
You can also use len()
on strings, since Python considers a string to be a type of sequence:
len("Supercalifragilisticexpialidocious") # Evaluates to: 34
The length of a string is typically the number of characters. In actuality, it's reporting the number of Unicode code points in a string, and Western alphabet letters require only one code point. However, many modern emojis require multiple Unicode code points, like 🤷🏽♀️, so len("🤷🏽♀️")
will actually report 5. I love emojis, but dang, programming with them is a pain.