Intro to Lists
Lists are probably the most popular collections used in the Python programming language. They're similar to "arrays" in other programming languages.
A list is an ordered, heterogeneous, mutable collection. This means:
As you add new elements, the list "remembers" the order that you chose for your elements. Example:
shopping_list =  # An empty list shopping_list.append('Eggs') shopping_list.append('Milk') shopping_list.append('Bread') print(shopping_list) # ['Eggs', 'Milk', 'Bread']
In the previous example you can see that the order of the elements in the resulting list (
['Eggs', 'Milk', 'Bread']) is the same order we chose when building the list ("Eggs" first, "Milk" second and finally "Bread").
This means that you can put any type of element in a list. This is a recurrent theme in all the other Python collections, they take any type of element. Example:
my_list = ["Hello", 3, True, 2.5]
As you can see, we're storing a string object (
"Hello"), an integer (
3), a boolean (
True) and a float (
2.5). Python lists are perfectly capable of handling any type of elements. Even other lists (we'll talk more about this later).
This means that we can change lists (we can mutate them). It probably sounds a little bit dumb to mention, but you'll see later that there are other collections that are not mutable. But going back to lists, you can simply change them at will, for example:
shopping_list =  # An empty list shopping_list.append('Eggs') # Add an element (changed the list) shopping_list.append('Milk') # Add another element print(shopping_list) # ['Eggs', 'Milk'] shopping_list.remove('Milk') # Remove an element (changed it again) print(shopping_list) # ['Eggs']
Don't overthink this; it's the most intuitive way of thinking collections: being mutable.
Having a list created is not enough. We'll need to "manipulate" these lists to achieve something useful. These are the most common operations with lists:
- Adding elements
- Counting elements ("How many elements does this list have?")
- Removing elements
- Check if an element exists in a list ("Is firstname.lastname@example.org already part of this list?")
- "Concatenating" lists: Building a new list out of two or more lists.
We'll explore all these operations in detail in the following lessons.