Section 7: Lists
Welcome to Section 7! In this section, we will cover the basics of lists. Let's get started!
7.1 Creation
As coders, we often need a place where we can keep multiple different data types together in one group. To do that, we can use lists. Here are some examples ...
list_of_numbers = [1, 2, 3, 4, 5]
list_of_strings = ["s", "t", "r", "i", "n", "g"]
list_of_floats = [1.2, 3.45, 6.789]
list_of_booleans = [True, False]
list_of_all = [12345, "string", 1234.56789, True]
The code above creates 5 different lists, 4 with one data type and 1 with many data types. Lists can be helpful on many ways by storing data. The syntax to create a list is simple. You first have to type a list name (follows the same rules as a variable name), an = sign, and two brackets []. Inside the brackets you can enter whatever data you want if they are separated by commas. You can then call the list as you would call a variable.
Now that you have list creation down, let's move on to the next part!
7.2 List
Python has a really useful function called list. This function can make a string into a list. Here is an example ...
print(list("string"))
This function will print ["s", "t", "r", "i", "n", "g"]. This is because the function list will make split every letter in a string into an item in a list. The syntax for the function list is the same as the syntax for all other string functions.
Now that you have the list function down, let's move on to the next part!
7.3 In and Not In
Sometimes, we will want to check if an item is in a list. To do that, we can use the in operator. Here are some examples ...
numbers = [1, 2, 3, 4]
print(1 in numbers)
print(5 in numbers)
The second line of the code above will print True. This is because 1 is in numbers. The last line of the code above will print False. This is because 5 is not in numbers. The syntax for the in operator is simple. You have to type a data point (number, string, float, or boolean), type in, and then type the name of a list.
Other times, we will want to check is an item is not in a list. To do that, we can use the not in operator. Here are some examples ...
numbers = [1, 2, 3, 4]
print(1 not in numbers)
print(5 not in numbers)
The second line of the code above will print False. This is because 1 is in numbers. The last line of the code above will print True. This is because 5 is not in numbers. The syntax for the not in operator is simple. You have to type a data point (number, string, float, or boolean), type not in, and then type the name of a list.
Now that you have the in and not in operators down, let's move on to the next part!
7.4 Indexes
Just like strings, lists also have indexes that start from 0 and increase by 1. Using list indexes, we can access different items in the list. Here are some examples ...
numbers = [1, 2, 3, 4, 5]
print(numbers[1])
print(numbers[3])
The second line of the code above will print 2 because the index one results in the second item in a list. The last line of the code above will print 4 because the index three results in the fourth item in a list. The syntax for indexing in lists is the same as indexing in strings.
Sometimes, we have a list inside of a list and he want to index an item inside the smaller list. To do that we can use a more advanced form of list indexing. Here are some examples ...
numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(numbers[1])
print(numbers[0][2])
The second line of the code above will print [4, 5, 6]. This is because the second item of the list numbers is actually a smaller list. The last line of the code above will print 3. This is because the first set of brackets gives us [1, 2, 3], and the second set of brackets will give us 3, the third value in [1, 2, 3]. The syntax is the same as before except there is one more set of brackets in the last line of the code above which do the same thing as the brackets in the previous part do.
As coders, we often want to be able to access the last or second to last value in a list. To do that, we can use negative indexes. Here are some examples ...
numbers = [1, 2, 3, 4]
print(numbers[-1])
print(numbers[-3])
When using negative indexes, you will start with -1 instead of 0 and increment by -1 instead of 1. -1 will be the last number and -3 the third to last number. This means that the second line of the code above will print 4 and the last line of the code above will print 2.
Now that you have indexes down, let's move on to the next part!
7.5 Slicing
Just like strings, lists can be sliced into many parts the same way. Here is an example ...
numbers = [1, 2, 3, 4]
print(numbers[1:2])
print(numbers[0:3])
The second line of the code above will print [2] as it will start on the second number and end before the third. The last line of the code above will print [1, 2, 3] as it will start on the first number and end before the fourth. The syntax for list slicing is the same as the syntax for string slicing, but instead of characters, items in the list are being printed.
Using list slicing, we can also replace certain parts of lists with different items. Here is an example ...
numbers = [4, 2, 1, 3]
numbers[0] = 1
print(numbers)
numbers[2:] = [3, 4]
print(numbers)
The second line of the code above sets the first number in the list to 1, so the line after that will print [1, 2, 3, 1]. The second to last line of the code above sets the last two numbers in the list to 3 and 4, respectively, so the line after that will print [1, 2, 3, 4]. The first step is to type the list name, two brackets, and indexes between the brackets. Then set that equal to the item or list that you want to replace those indexes with.
Now that you have list slicing down, let's move on to the next section!
Sample Problems