List Basics

Lists in Python are ordered collections of things. Lists and list elements are designated by square brackets, [].

>>> pycon_feelings = ['Hooray', 'Good', 'Amazing', 'Lacking bees']
>>> pycon_feelings
['Hooray', 'Good', 'Amazing', 'Lacking bees']

Lists can contain of all types of things; they don’t have to have all strings, integers, etc.

>>> things = [5, True, "hello"]
>>> things
[5, True, 'hello']

You can even put lists of things in other lists!

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

Just like every other object, we can use type to confirm that we’re working with a list:

>>> type(things)
<class 'list'>

We can also use conditionals on lists:

>>> 5 in things
>>> False in things
>>> "hello" in things

Your Turn: Lists 🏁

List Exercises

Guest List

Make a program that asks the user for their name, checks a list of names and either tells them “Welcome THEIR_NAME!” or “Sorry, but you’re not on the guest list”.

Virtual Friend

Create a program that does the following:

  1. Asks how you are
  2. Compares the input to a list of good emotions and a list of bad emotions, and prints an appropriate response depending on which list the input is in
  3. Prints “I truly don’t know what that is like.” if the input isn’t in either list
  4. Chooses a random response if the input “How are you” is received (with any capitalization)

Lazy Chef Sandwich Generator

You’ve just opened a restaurant but you’re too lazy to come up with a sandwich menu. Make a program to do it instead!

Create a program that does the following:

  1. Create a list of options for each element of the sandwich (proteins, spreads, bread, etc.).
  2. Print one random item from each of the sandwich elements lists when the program is run.

Example usage:

$ python3.6
white bread


You can use random.choice() to choose an item at random.

Acquiring Orange, Inc.

You’ve acquired the famous tech company Orange, Inc.! Time to combine your assets.

  1. Make a list my_assets containing your assets, and a list orange_assets containing tech giant Orange, Inc.’s assets
  2. Make a new list my_orange_assets that contains all of the combined assets of the previous lists


This shouldn’t be a list of two lists, but rather a list containing all of the items from the first two lists. Do you remember how we combined strings? See if something like that will work here.

Interactive Rock, Paper, Scissors

Update your rock, paper, scissors program to play against the computer. It should work like this:

  1. Prompt the user for a move
  2. Choose a move at random for the computer
  3. Print what the computer chose and who won


random.choice() will help here. See the documentation for more information.

Magic 8 Ball

Make a program that takes a yes or no question as and gives a yes, no, or maybe response at random.