In this chapter we'll learn about lists.
A Python list is a collection of items.
The items in a Python list can be of any data type and the items contained in a list can even be other lists, dictionaries, sets, tuples, or any mixture of Python data.
Lists are mutable, which means that the item inside of a list can be retrieved, changed, rearranged, and acted upon in multiple different ways.
Python lists maintain their order, which means that you can access something by its position or index using some of the list methods available in Python.
In this exercise, we will create several lists and we will use the Python list methods to interact with the list.
Let's go ahead and get started.
We'll get started by making some basic lists that we can work with throughout the exercise.
Our first list will be a list of fruits.
You'll notice that the syntax for a list includes the variable, which is the name of the list, a single equal sign and square brackets that contain the items of the list.
In our second list will include numbers as well as Strings.
You'll notice that I've still used the square brackets and each item is separated by a comma.
You'll also notice that in the middle of the list I've used a decimal or floating-point number.
Let's start working with some of the list methods.
Python includes several methods to work with lists, and the first method that we'll work with is the append
method.
Before we add this method, let's go ahead and print our lists, so that way that we can make sure the output is as we expect.
With the append
method, I use the name of the list; .append
and then I can add a single item to the list.
fruits = ["peaches", "pears", "apples"]
years = [3, "1998", 2.5, 987, "1994"]
print(fruits, years)
fruits.append("oranges")
print(fruits)
We'll go ahead and print and see if the oranges are added to our list.
So yes, our initial list of fruits only has 3 items.
Our new list has four items and oranges are in that list of items.
What if you want to add multiple items to a list?
You can do so by using the list extend
method. Our list extend
method allows us to extend the list with another list.
We call the fruit.extend
method and then we include the list that we'd like to extend.
fruits.extend(years)
print(fruits)
When we print our list of fruits, we should also have the items contained in the years list within the single fruits list.
And you'll notice this is true.
Our list after the extend
function includes all of the fruits as well as all the integers, decimals, and year Strings.
So, we've worked with methods to add items to a list; now let's work with the methods that remove items from a list.
The first method for taking an item out of a list is the list remove
method.
It's important to note that when you use the method, you need to make sure that you are actually using the exact item match that you want to remove.
For example, we have “oranges” in the list, but if I say remove “orange” and then I print the list, we will get an error.
And we get the error because “orange” is not in the list.
So, you need to make sure that when you're using the remove method that you use an exact match.
If we change it to “oranges”:
fruits.remove("oranges")
print(fruits)
And rerun the code, then you'll notice that oranges has been removed from the list.
Another way to remove elements from a list is to remove an element by index.
An index is the number that is assigned to each item in the list. The index starts at 0 and most Python lists and Python counting begin at 0.
If we want to remove the first item from a list, then we will use the pop
method and indicate which items.
fruits.pop(0)
print(fruits)
As I said, Python lists begin at 0.
You'll notice that the first item (“peaches”), the 0 item was removed from the list.
If you want to access the last item in a list and you're not certain of the length of the list, you can use the concept of negative indexing, which starts at the end of a list.
So, if I want to remove the last item, I can use fruits.pop(-1)
.
Another method available for lists is the sort
method.
It's important to note that the sort
method can only be used with lists of like types.
For example, if we tried to sort our fruits list, we will actually get an error.
Python is unable to compare the list items in any way to sort them.
Let's try a list with like items and see how the sorting goes.
I'll comment out the fruit lines so that way that they don't cause an error that prevents our numbers code from running.
# fruits.sort()
# print(fruits)
numbers = [5, 1928, 4, 17, 68, 73.76, 20.458]
numbers.sort()
print(numbers)
You'll notice that the sort
function sorted our list of numbers.
In a numbers list, you can combine integers and floating-point numbers and they'll be sorted.
NOTE
I’m going to go ahead and clear out my console and I'm also going to go ahead and clear out my code file. I do want to use the fruits and years lists, but I don't want to see any more of the print codes. So, you can start a multi-line comment at the top and end the multi-line comment at the bottom which comments out the code that we don't want to run. And I'm doing this so that way that I have some real estate to show you some of the new things that we're going to do with the lists.
The first new thing that we're going to do is check membership in a list.
Python has a function called in
and this function allows us to check membership.
So, I can do this simply by giving the command to print a member of the list in the total list. The results will be either True or False.
fruits = ["peaches", "apples", "pears", "apples", "apples"]
years = [3, "1998", 2.5, 987, "1994"]
print("apple" in fruits)
In this example code, the result is False because it is not an exact match, but if we use ”apples” in fruits
it will be True.
Using in
is a simple way to check membership if you are working with a large dataset and you aren't sure if the item is in the list or not.
You can also check membership and the number of items with the count
function.
print(fruits.count("apples"))
You'll notice I have 1 instance of apples.
I'm going to change my list and I'm adding 3 instances of the word “apples”.
I'll go ahead and run my code again and now you'll notice that the count
function tells me that the item is there and 3 instances of the word “apples”.
If the item does not exist — for example, print(fruits.count("apple"))
— it will tell me that there are 0 instances of that item in the list.
We can also check membership as well as the index position using the index
function.
print(fruits.index("apples"))
The first instance of “apples” appears as the second item in the list.
I encourage you to keep experimenting with these list methods on lists of different types and sizes.In this chapter we'll learn about lists.
A Python list is a collection of items.
The items in a Python list can be of any data type and the items contained in a list can even be other lists, dictionaries, sets, tuples, or any mixture of Python data.
Lists are mutable, which means that the item inside of a list can be retrieved, changed, rearranged, and acted upon in multiple different ways.
Python lists maintain their order, which means that you can access something by its position or index using some of the list methods available in Python.
In this exercise, we will create several lists and we will use the Python list methods to interact with the list.
Let's go ahead and get started.
We'll get started by making some basic lists that we can work with throughout the exercise.
Our first list will be a list of fruits.
You'll notice that the syntax for a list includes the variable, which is the name of the list, a single equal sign and square brackets that contain the items of the list.
In our second list will include numbers as well as Strings.
You'll notice that I've still used the square brackets and each item is separated by a comma.
You'll also notice that in the middle of the list I've used a decimal or floating-point number.
Let's start working with some of the list methods.
Python includes several methods to work with lists, and the first method that we'll work with is the append
method.
Before we add this method, let's go ahead and print our lists, so that way that we can make sure the output is as we expect.
With the append
method, I use the name of the list; .append
and then I can add a single item to the list.
fruits = ["peaches", "pears", "apples"]
years = [3, "1998", 2.5, 987, "1994"]
print(fruits, years)
fruits.append("oranges")
print(fruits)
We'll go ahead and print and see if the oranges are added to our list.
So yes, our initial list of fruits only has 3 items.
Our new list has 4 items and oranges are in that list of items.
What if you want to add multiple items to a list?
You can do so by using the list extend
method. Our list extend
method allows us to extend the list with another list.
We call the fruit.extend
method and then we include the list that we'd like to extend.
fruits.extend(years)
print(fruits)
When we print our list of fruits, we should also have the items contained in the years list within the single fruits list.
And you'll notice this is true.
Our list after the extend
function includes all of the fruits as well as all the integers, decimals, and year Strings.
So, we've worked with methods to add items to a list; now let's work with the methods that remove items from a list.
The first method for taking an item out of a list is the list remove
method.
It's important to note that when you use the method, you need to make sure that you are actually using the exact item match that you want to remove.
For example, we have “oranges” in the list, but if I say remove “orange” and then I print the list, we will get an error.
And we get the error because “orange” is not in the list.
So, you need to make sure that when you're using the remove method that you use an exact match.
If we change it to “oranges”:
fruits.remove("oranges")
print(fruits)
And rerun the code, then you'll notice that oranges has been removed from the list.
Another way to remove elements from a list is to remove an element by index.
An index is the number that is assigned to each item in the list. The index starts at 0 and most Python lists and Python counting begin at 0.
If we want to remove the first item from a list, then we will use the pop
method and indicate which items.
fruits.pop(0)
print(fruits)
As I said, Python lists begin at 0.
You'll notice that the first item (“peaches”), the 0 item was removed from the list.
If you want to access the last item in a list and you're not certain of the length of the list, you can use the concept of negative indexing, which starts at the end of a list.
So, if I want to remove the last item, I can use fruits.pop(-1)
.
Another method available for lists is the sort
method.
It's important to note that the sort
method can only be used with lists of like types.
For example, if we tried to sort our fruits list, we will actually get an error.
Python is unable to compare the list items in any way to sort them.
Let's try a list with like items and see how the sorting goes.
I'll comment out the fruit lines so that way that they don't cause an error that prevents our numbers code from running.
# fruits.sort()
# print(fruits)
numbers = [5, 1928, 4, 17, 68, 73.76, 20.458]
numbers.sort()
print(numbers)
You'll notice that the sort
function sorted our list of numbers.
In a numbers list, you can combine integers and floating-point numbers and they'll be sorted.
NOTE
I’m going to go ahead and clear out my console and I'm also going to go ahead and clear out my code file. I do want to use the fruits and years lists, but I don't want to see any more of the print codes. So, you can start a multi-line comment at the top and end the multi-line comment at the bottom which comments out the code that we don't want to run. And I'm doing this so that way that I have some real estate to show you some of the new things that we're going to do with the lists.
The first new thing that we're going to do is check membership in a list.
Python has a function called in
and this function allows us to check membership.
So, I can do this simply by giving the command to print a member of the list in the total list. The results will be either True or False.
fruits = ["peaches", "apples", "pears", "apples", "apples"]
years = [3, "1998", 2.5, 987, "1994"]
print("apple" in fruits)
In this example code, the result is False because it is not an exact match, but if we use ”apples” in fruits
it will be True.
Using in
is a simple way to check membership if you are working with a large dataset and you aren't sure if the item is in the list or not.
You can also check membership and the number of items with the count
function.
print(fruits.count("apples"))
You'll notice I have 1 instance of apples.
I'm going to change my list and I'm adding 3 instances of the word “apples”.
I'll go ahead and run my code again and now you'll notice that the count
function tells me that the item is there and 3 instances of the word “apples”.
If the item does not exist — for example, print(fruits.count("apple"))
— it will tell me that there are 0 instances of that item in the list.
We can also check membership as well as the index position using the index
function.
print(fruits.index("apples"))
The first instance of “apples” appears as the second item in the list.
I encourage you to keep experimenting with these list methods on lists of different types and sizes.