knowt logo

PYTHON LESSON 5: Python Lists & Dictionaries (Part 1)

Introduction to Lists;

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

list_name = [item_1, item_2]

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.

Access by Index;

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.

New Neighbours;

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:

zoo_animals[0]
# Gets the value "pangolin"

You can see how assignment works on line 5:

zoo_animals[2] = "hyena"
# Changes "sloth" to "hyena"

Late Arrivals & List Strength;

A list doesn’t have to have a fixed length. You can add items to the end of a list any time you like!

letters = ['a', 'b', 'c']
letters.append('d')
print len(letters)
print letters

  1. In the above example, we first create a list called letters.

  2. Then, we add the string 'd' to the end of the letters list.

  3. Next, we print out 4, the length of the letters list.

  4. Finally, we print out ['a', 'b', 'c', 'd'].

List Slicing;

Sometimes, you only want to access a portion of a list. Consider the following code:

letters = ['a', 'b', 'c', 'd', 'e']
slice = letters[1:3]
print slice
print letters

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.

Slicing Lists & Strings;

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.

my_list[:2]
# Grabs the first two items
my_list[3:]
# Grabs the fourth through last items

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.

Maintaining Order;

Sometimes you need to search for an item in a list.

animals = ["ant", "bat", "cat"]
print animals.index("bat")

  1. First, we create a list called animals with three strings.

  2. Then, we print the first index that contains the string "bat", which will print 1.

We can also insert items into a list.

animals.insert(1, "dog")
print animals

  1. We insert "dog" at index 1, which moves everything down by 1.

  2. We print out ["ant", "dog", "bat", "cat"]

IC

PYTHON LESSON 5: Python Lists & Dictionaries (Part 1)

Introduction to Lists;

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

list_name = [item_1, item_2]

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.

Access by Index;

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.

New Neighbours;

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:

zoo_animals[0]
# Gets the value "pangolin"

You can see how assignment works on line 5:

zoo_animals[2] = "hyena"
# Changes "sloth" to "hyena"

Late Arrivals & List Strength;

A list doesn’t have to have a fixed length. You can add items to the end of a list any time you like!

letters = ['a', 'b', 'c']
letters.append('d')
print len(letters)
print letters

  1. In the above example, we first create a list called letters.

  2. Then, we add the string 'd' to the end of the letters list.

  3. Next, we print out 4, the length of the letters list.

  4. Finally, we print out ['a', 'b', 'c', 'd'].

List Slicing;

Sometimes, you only want to access a portion of a list. Consider the following code:

letters = ['a', 'b', 'c', 'd', 'e']
slice = letters[1:3]
print slice
print letters

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.

Slicing Lists & Strings;

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.

my_list[:2]
# Grabs the first two items
my_list[3:]
# Grabs the fourth through last items

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.

Maintaining Order;

Sometimes you need to search for an item in a list.

animals = ["ant", "bat", "cat"]
print animals.index("bat")

  1. First, we create a list called animals with three strings.

  2. Then, we print the first index that contains the string "bat", which will print 1.

We can also insert items into a list.

animals.insert(1, "dog")
print animals

  1. We insert "dog" at index 1, which moves everything down by 1.

  2. We print out ["ant", "dog", "bat", "cat"]

robot