# List

## What is a list?

A list is an ordered set of objects in Python. Suppose we want to make a list of the heights of students in a class:

• Jenny is 61 inches tall
• Alexus is 70 inches tall
• Sam is 67 inches tall
• Grace is 64 inches tall

In Python, we can create a variable called heights to store these numbers:

heights = [61, 70, 67, 64]

Notice that:

1. A list begins and ends with square brackets ([ and ]).
2. Each item (i.e., 67 or 70) is separated by a comma (,)
3. It’s considered good practice to insert a space () after each comma, but your code will run just fine if you forget the space.

Lists can contain more than just numbers.

• Jenny is 61 inches tall
• Alexus is 70 inches tall
• Sam is 67 inches tall
• Grace is 64 inches tall

We can make a list of strings that contain the students’ names:

names = [‘Jenny’, ‘Alexus’, ‘Sam’, ‘Grace’]

We can also combine multiple data types in one list. For example, this list contains both a string and an integer:

mixed_list = [‘Jenny’, 61]

## List of Lists

We’ve seen that the items in a list can be numbers or strings. They can also be other lists!

• Jenny is 61 inches tall
• Alexus is 70 inches tall
• Sam is 67 inches tall
• Grace is 64 inches tall

Previously, we saw that we could create a list representing both Jenny’s name and height:

jenny = [‘Jenny’, 61]

We can put several of these lists into one list, such that each entry in the list represents a student and their height:

heights = [[‘Jenny’, 61], [‘Alexus’, 70], [‘Sam’, 67], [‘Grace’, 64]]

## Zip

• Jenny is 61 inches tall
• Alexus is 70 inches tall
• Sam is 67 inches tall
• Grace is 64 inches tall

Suppose that we already had a list of names and a list of heights:

 names = [‘Jenny’, ‘Alexus’, ‘Sam’, ‘Grace’] heights = [61, 70, 67, 65]

If we wanted to create a list of lists that paired each name with a height, we could use the command zip. The zip takes two (or more) lists as inputs and returns an object that contains a list of pairs. Each pair contains one element from each of the inputs. You won’t be able to see much about this object from just printing it:

 names_and_heights = zip(names, heights) print(names_and_heights)

because it will return the location of this object in memory. Output would look something like this:



To see the nested lists, you can convert the zip object to a listfirst:

 print(list(names_and_heights))

returns:

 [(‘Jenny’, 61), (‘Alexus’, 70), (‘Sam’, 67), (‘Grace’, 65)]

## Empty Lists

A list doesn’t have to contain anything! You can create an empty list like this:

 empty_list = []

Why would we create an empty list?

Usually, it’s because we’re planning on filling it later based on some other input. We’ll talk about two ways of filling up a list in the next exercise.

## Growing a List: Append

We can add a single element to a list using .append(). For example, suppose we have an empty list called empty_list:

 empty_list = []

We can add the element 1 using the following commands:

 empty_list.append(1)

If we examine empty_list, we see that it now contains 1:

 >>> print(empty_list) 

When we use .append() on a list that already has elements, our new element is added to the end of the list:

 # Create a list my_list = [1, 2, 3] # Append a number my_list.append(5) print(my_list) # check the result

the output looks like:

[1, 2, 3, 5]

It’s important to remember that .append()comes after the list. This is different from functions like print, which come before.

## Growing a List: Plus (+)

When we want to add multiple items to a list, we can use + to combine two lists.

Below, we have a list of items sold at a bakery called items_sold:

Suppose the bakery wants to start selling ‘biscuit’ and ‘tart’:

>>>

 items_sold_new = items_sold + [‘biscuit’, ‘tart’] >>> print(items_sold_new) [‘cake’, ‘cookie’, ‘bread’, ‘biscuit’, ‘tart’]

In this example, we created a new variable, items_sold_new, which contained both the original items sold, and the new ones. We can inspect the original items_sold and see that it did not change:

We can only use + with other lists. If we type in this code:

 my_list = [1, 2, 3] my_list + 4

we will get the following error:

 TypeError: can only concatenate list (not “int”) to list

If we want to add a single element using +, we have to put it into a list with brackets ([]):

 my_list + 

## Range

Often, we want to create a list of consecutive numbers. For example, suppose we want a list containing the numbers 0 through 9:

 my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Typing out all of those numbers takes time and the more numbers we type, the more likely it is that we have a typo.

Python gives us an easy way of creating these lists using a function called range. The function range takes a single input, and generates numbers starting at 0 and ending at the number before the input. So, if we want the numbers from 0 through 9, we use range(10) because 10 is 1 greater than 9:

 my_range = range(10)

Just like with zip, the range function returns an object that we can convert into a list:

 >>> print(my_range) range(0, 10) >>> print(list(my_range)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We can use range to generate more interesting lists.

By default, range creates a list starting at 0. However, if we call range with two arguments, we can create a list that starts at a different number. For example, range(2, 9)would generate numbers starting at 2 and ending at 8 (just before 9):

 >>> my_list = range(2, 9) >>> print(list(my_list)) [2, 3, 4, 5, 6, 7, 8]

With one or two arguments, range will create a list of consecutive numbers (i.e., each number is one greater than the previous number). If we use a third argument, we can create a list that “skips” numbers. For example, range(2, 9, 2) will give us a list where each number is 2 greater than the previous number:

 >>> my_range2 = range(2, 9, 2) >>> print(list(my_range2)) [2, 4, 6, 8]

We can skip as many numbers as we want! In this example, we’ll start at 1 and skip 10between each number until we get to 100:

 >>> my_range3 = range(1, 100, 10) >>> print(list(my_range3)) [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]

Our list stops at 91 because the next number in the sequence would be 101, which is greater than 100 (our stopping point).

## Review

So far, we have learned

• How to create a list
• How to create a list of lists using zip
• How to add elements to a list using either .append() or +
• How to use range to create lists of integers

Let’s practice these skills.