PYTHON LESSON 5: Python Lists & Dictionaries (Part 1)
Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name. (Datatypes you’ve already learned about include strings, numbers, and booleans.) You can assign items to a list with an expression of the form
|
---|
with the items in between brackets. A list can also be empty: empty_list = []. Lists are very similar to strings, but there are a few key differences.
You can access an individual item on the list by its index. An index is like an address that identifies the item’s place in the list. The index appears directly after the list name, in between brackets, like this: list_name[index].
List indices begin with 0, not 1! You access the first item in a list like this: list_name[0]. The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero.
A list index behaves like any other variable name! It can be used to access as well as assign values.
You saw how to access a list index like this:
|
---|
You can see how assignment works on line 5:
|
---|
A list doesn’t have to have a fixed length. You can add items to the end of a list any time you like!
|
---|
In the above example, we first create a list called letters.
Then, we add the string 'd' to the end of the letters list.
Next, we print out 4, the length of the letters list.
Finally, we print out ['a', 'b', 'c', 'd'].
Sometimes, you only want to access a portion of a list. Consider the following code:
|
---|
What is this code doing?
First, we create a list called letters.
Then, we take a subsection of the list and store it in the slice list. We do this by defining the indices we want to include after the name of the list: letters[1:3]. In Python, when we specify a portion of a list in this manner, we include the element with the first index, 1, but we exclude the element with the second index, 3.
Next, we print out slice, which will print ['b','c']. Remember, in Python indices always start at 0, so the 1 element is actually b.
Finally, we print out ['a', 'b', 'c', 'd', 'e'], notice that we did not modify the original letters list.
You can slice a string exactly like a list! In fact, you can think of strings as lists of characters: each character is a sequential item in the list, starting from index 0.
|
---|
If your list slice includes the very first or last item in a list (or a string), the index for that item doesn’t have to be included.
Sometimes you need to search for an item in a list.
|
---|
First, we create a list called animals with three strings.
Then, we print the first index that contains the string "bat", which will print 1.
We can also insert items into a list.
|
---|
We insert "dog" at index 1, which moves everything down by 1.
We print out ["ant", "dog", "bat", "cat"]
Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name. (Datatypes you’ve already learned about include strings, numbers, and booleans.) You can assign items to a list with an expression of the form
|
---|
with the items in between brackets. A list can also be empty: empty_list = []. Lists are very similar to strings, but there are a few key differences.
You can access an individual item on the list by its index. An index is like an address that identifies the item’s place in the list. The index appears directly after the list name, in between brackets, like this: list_name[index].
List indices begin with 0, not 1! You access the first item in a list like this: list_name[0]. The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero.
A list index behaves like any other variable name! It can be used to access as well as assign values.
You saw how to access a list index like this:
|
---|
You can see how assignment works on line 5:
|
---|
A list doesn’t have to have a fixed length. You can add items to the end of a list any time you like!
|
---|
In the above example, we first create a list called letters.
Then, we add the string 'd' to the end of the letters list.
Next, we print out 4, the length of the letters list.
Finally, we print out ['a', 'b', 'c', 'd'].
Sometimes, you only want to access a portion of a list. Consider the following code:
|
---|
What is this code doing?
First, we create a list called letters.
Then, we take a subsection of the list and store it in the slice list. We do this by defining the indices we want to include after the name of the list: letters[1:3]. In Python, when we specify a portion of a list in this manner, we include the element with the first index, 1, but we exclude the element with the second index, 3.
Next, we print out slice, which will print ['b','c']. Remember, in Python indices always start at 0, so the 1 element is actually b.
Finally, we print out ['a', 'b', 'c', 'd', 'e'], notice that we did not modify the original letters list.
You can slice a string exactly like a list! In fact, you can think of strings as lists of characters: each character is a sequential item in the list, starting from index 0.
|
---|
If your list slice includes the very first or last item in a list (or a string), the index for that item doesn’t have to be included.
Sometimes you need to search for an item in a list.
|
---|
First, we create a list called animals with three strings.
Then, we print the first index that contains the string "bat", which will print 1.
We can also insert items into a list.
|
---|
We insert "dog" at index 1, which moves everything down by 1.
We print out ["ant", "dog", "bat", "cat"]