Skip to content

Introduction to Python – Chapter 7 – Lists

In this video, we’ll talk about lists.

List is the term used for an ordered sequence of values where each value has an index.

Before we continue to talk about lists, there is also a term called arrays from other programming languages that you might have heard about.

Arrays are almost exactly like lists in Python. There’s an array module in Python for using arrays.

In Python, arrays can only hold data of the same type. Lists can hold multiple data types.

Python arrays are better than lists memory-wise. But in Python, lists are better performance-wise than arrays.

If you know about arrays from a different programming language, use lists in Python because lists in Python are faster than arrays.

Most of the time, speed is more important than memory when talking about these data structures.

Usually when people say “array” when talking about Python, they mean “list” because lists are fundamental to Python the same way arrays are fundamental to other programming languages.

When we talk about a list, just think of any list of values like a shopping list or a checklist.

Every value inside a list has an index.

An index is the numbered position of a value in a list.

The first value in a list starts at index, 0. The second value has index, 1, and so on.

The actual values in a list are called elements.

Lists are like strings in the way that they can be broken up into their individual parts.

The difference between a list and a string is that the values in a list can be of any type.

Let’s make a list to demonstrate. To make a list, you use a couple of brackets.

In those brackets, you include each value and separate each value by a comma.

In Python, the values of a list don’t have to have the same type.

We can mix values of different types in a list like:

When you put a list in a list, the list inside the list is called a nested list.

We can print out the lists.

When we run the program, the lists are printed with their values.

We can make a list without any elements. This type of list is called an empty list.

Let’s talk about how you can access a list.

You can access each element of a list with its index.

If I wanted the first element of example_list, which is 1, I would write:

When I run the program, I get the 1, because the first value of a list starts at index, 0.

You simply write a pair of brackets and put the index in between the brackets.

You can use the len() function on a list the same way that you could use the len() function with a string.

The last value in a list has the index, len(list_name) – 1.

We will get the value, 4, because it’s the last value in the list.

You can also use -1 to represent the last index in the list, but most people don’t prefer to use the negative numbers to represent the indices.

Like with strings, if we were to access an index of a list that does not exist, we’d get an IndexError.

There is no index that is the length of the list, example_list, so when we run the program, we get an IndexError.

Let’s talk about using iteration and loops with lists.

The while loops and for loops that we talked about in previous videos can be used with lists to visit and get the values of a list.

You might want to visit every value in a list.

If you’re visiting every value in the list, then you should use a for loop because there’s a counting process where you have a start position progressing toward an end position.

I’ll repeat the most popular for loop concepts that we talked about in the last video.

The most popular way of using a for loop with a list is visiting every value of the list.

This for loop is the simplest to make. It uses the for keyword followed by a variable that you name.

I used i in example_list, which means i will take the value of each value of the list at each step.

When you want to create this type of for loop, the general setup is always:

If you read the for loop in English, it becomes easier to understand.

for every variable in list, do something to the variable.

When I run the program, I get the contents of the list printed out line by line like how we printed out the characters of a string in the last video.

The second popular way of traversing a list with a for loop is using enumerate keyword.

Using enumerate keyword allows you to not only get the value of each position in the list, but also get the index.

For example, we can write:

We use the for keyword followed by two variables this time around. I named the variables index and value. Then, in enumerate the list.

enumerate allows you to retrieve both the index and value in that order, which is why I used the variables, index comma value.

When we run the program, we’ll get the index followed by the value printed out.

Let’s talking about checking if a value is in a list. Checking for membership.

Let’s say we wanted to check if “microwave” was in this list.

When we run the program, since “microwave” is inside appliances, we get “microwave in appliances”.

You can use operators on lists.

Let’s say that I wanted to combine two lists.

c becomes a new list with the contents of a and b.

The list will be [1, 2, 3, 4].

When I run the program, I get the new list with a and b’s list values.

The multiply operator can repeat the contents of a list.

For example, let’s say that I wanted to multiply a’s contents by 3 times.

When I run the program, I get [1, 2, 1, 2, 1, 2]

The values inside a, which were originally 1, 2 are tripled because I multiplied a by 3.

We call using operators on lists, list operations.

In the last video, we talked about string slicing, which was the ability to grab a subsection of a string without using a for loop.

Lists can also use slicing.

If I wanted to get a list with a, b, and c, I would use list slicing.

How list slicing works is you have a start position followed by a colon followed by the end position + 1.

The start position 0 in list slicing is inclusive. example_list at index 0, which is “a” is included in the new list.

Then, the list slicing visits every value in between start position until end position.

The end position 3 is non-inclusive, which means that example_list at index 2, which is “c” is included in the new list, but example_list at index 3 is not included. 0 is inclusive. 3 is non-inclusive.

When we run the program, I’ll get the half_list, which is a new list with “a”, “b”, and “c”.

In the last video, we talked about how strings were immutable, meaning that we could not change the characters of a string with different characters.

Lists are mutable in Python. We can change the elements of a list.

The first value of the list is changed to a “b”.

You can also do fancy things like use list slicing to assign new values.

For example, I could use list slicing to change the first three values of the list.

example_list[0:3] = [“x”, “y”, “z”]

Now, when I print example_list, the first three values will get the x, y, z because I used string slicing to get the first three values then I reassigned them with x, y, z.

I can run the program, and I’ll see that the program has the “x”, “y”, “z”, “d”, “e”, “f”.

Let’s say that we wanted to add new values to the list.

There’s a very useful append function that lists can use to add new values to the end of a list.

When I run the program, we can see that the last three values of the list are g, h, and i, which were added with the .append function.

Let’s say that we wanted to sort the values in a list.

example_list = [“b”, “a”, “e”, “c”, “f”, “d”]

You can use the list’s sort function.

Sorting a list can be very useful. All you need to do is add a .sort().

When we run the program, we can see that the values in the list have been sorted in alphabetical order.

Another very useful list function is reversing a list.

Let’s take that sorted_list and reverse it.

All you need to do is add a .reverse() to the list. When we run the program, we get the reversed list.

The last thing that we’ll talk about is list deletion, which means that I can delete values in a list.

Let’s say that I wanted to delete the last value in the list.

First, I’ll get rid of example_list.reverse().

When I print example_list, “f”, which used to be the last value of the sorted list has been deleted.

There are more functions that you can use with lists, but we’ve talked about the most useful ones so far.

Google is your friend, so if you ever can’t figure out how to get a specific value from a list, Python might already have a built-in function that does exactly what you’re looking for.


Video Demonstration