Lists

List Basics

Remember when we did the compliments.py exercise? What was that sneaky thing in there with the brackets?

A list!

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

>>> tutorial_feelings = ['Hooray', 'Good', 'Amazing', 'Lacking bees']
>>> tutorial_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, tutorial_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
True
>>> False in things
False
>>> "hello" in things
True

Getting Things From Lists

In our compliments exercise we used the random module to grab an item out. But what if we want to do things on purpose?

One cool thing about lists is that you can grab certain items from the list by using slices or indices. 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]
1

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]
1

Neat!

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 a single item from a list within a list? Remember our list of things and PyCon feelings?

>>> more_things = [things, tutorial_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]
True

Awesome!

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)
'tigers'
>>> my_list
['bears']

Time to Play! Lists 🏓

  1. Open up the REPL (the one with the hungry reptile >>>) and make a list containing the gratitudes you wrote down before. (Or all new ones if you want to be industrious!)
  2. Add a gratitude to the list.
  3. Remove a gratitude from the list at random.