More Lists

Getting Things From Lists

One cool thing about lists is that you can grab certain items from the list by using slices or indicies. You don’t really need to remember those terms, but there are two things you should remember:

  1. The first item in the list isn’t 1, it’s 0.
  2. We get the item in the list that we want using [].

For example:

>>> [1, 2, 3, 4][0]

This way looks a little goofy and confusing, so let’s assign the list to a variable.

>>> favorite_numbers = [1, 2, 3, 4]
>>> favorite_numbers[0]


We can also get a portion of things from a list:

>>> favorite_numbers = [1, 2, 3, 4]
>>> favorite_numbers[0:2]
[1, 2]

Wait a second. We’ve asked for the items in the list from 0 to 2. The value at index 0 is 1, the value at index 1 is 2, and the value at index 2 is 3. So why did we only get [1, 2] back?

This is because the slice sytax (the way we get just a portion of items from a list) returns the first item in the slice up to, but not including the second value. Tricky.

What about getting things a single item from a list within a list? Remember our list of things and PyCon feelings?

>>> more_things = [things, pycon_feelings]
>>> more_things
[[5, True, 'hello'], ['Hooray', 'Good', 'Amazing', 'Lacking bees']]

How do we get True from the list things in more_things? Well, what does this give us?

>>> more_things[0]

It gives us the list things, since that’s what’s at index 0. Can we build on more_things[0] to get the second item in in _that_ list?

>>> more_things[0][1]


Modifying Lists

One last topic on lists: changing them.

You can add to a list like this:

>>> my_list = ["lions", "tigers", "bears"]
>>> my_list
['lions', 'tigers', 'bears']
>>> my_list.append("Oh my")
>>> my_list
['lions', 'tigers', 'bears', 'Oh my']

You can remove items a couple of ways.

You can remove a specific item by its value:

>>> my_list.remove("lions")
>>> my_list
['tigers', 'bears', 'Oh my']

You can remove and return the last item in the list using pop:

>>> my_list.pop()
'Oh my'
>>> my_list
['tigers', 'bears']

And you can remove and return an item by its index:

>>> my_list.pop(0)
>>> my_list

Your Turn: More Lists 🏁

More List Exercises

Random Vacation Planner

Create a program that plans five random vacations by:

  1. Asking for input for five locations
  2. Choose a random location and random duration
  3. Print the five destinations and number of days of the trip for each.

No destination should appear in more than one trip.

State Capital Guesser

Copy the contents of into a new file In this new file, create a program that does the following.

  1. Chooses a state to quiz at random
  2. Asks the user for a guess of the capital city of that state
  3. Prints “Yay! Good job!” if the answer is correct, and “Sorry, that’s incorrect.” if not.

Example usage:

$ python3.6
What is the capital of Montana? Helena
Yay! Great job!
$ python3.6
What is the capital of Georgia? Sacramento
Sorry, that's incorrect.

Better Lazy Chef

Expand your lazy chef program to do the following:

  1. Choose a name at random from a name of sandwiches and assign your random sandwich ingredients to that name. Your menu should not include two sandwiches with the same name.
  2. Print out your menu items in a nice, human-readable way.


If you didn’t make a lazy chef program previously, you can use these lists to start with:

proteins = ["ham", "turkey", "beef", "tofu"]
condiments = ["mustard", "mayo", "hummus"]
veggies = ["tomato", "lettuce", "onion", "sprouts"]
breads = ["white bread", "wheat bread", "pita bread", "sourdough"]

Example usage:

$ python3.6
The Terminator has turkey, lettuce, and mayo on rye bread.
The First Lady has tofu, tomato, and hummus on pita bread.
The San Diegan has tuna, sprouts, and dill spread on sourdough.

Acquiring Oranges’s First and Last Assets

  1. Make a list my_assets containing your assets, and a list orange_assets containing Orange, Inc.’s assets
  2. Make a new list my_orange_assets that contains all of your assets, and Orange’s first and last assets. Hopefully those are their best products!