Life is Like A Python List

Life is Like A Python List

Life is like a Python list. You spend most of it in boxes. Sometimes the box is empty and sometimes the box is filled with different objects. And sometimes it’s a box within a box. I like to think of Python lists as boxes because the brackets look like a container. We make an empty box, or list, like this:

But that’s not very useful. Let’s give it a name:

Some chocolates ship with a map displaying the type and location of chocolates within the box. This is our index. Let’s say I just picked up a box of Whizzo Chocolates. In Python, it would look like this:

The chocolate I want is ‘Crunchy Frog’. I access that delectable item by its index, like so:

Remember our discussion on counting in Strings? It works the same with Lists. We always start at 0. Here’s a quick refresher.

I named this example list t. It’s a convention you will encounter frequently. Why t? Because list is a keyword and l looks too much like 1 or I.

We can assign list items to variables, like this:

Or, to clarify the counting, look at it like this:

Try it with the other indices.

As long as the value is an integer, we can use a variable as an index to access an item. Let me pick a chocolate for you, at random:

Delicious, no? Life is like a box of Whizzo chocolates. You never know what you’re going to get.

Heterogeneity & Mutability & Nesting & What?

Unlike strings, lists are mutable. We can modify a list item through its index.

Did you see what I did there? I swapped the value of 3 with the string ‘three’. Lists are heterogeneous, which means “composed of parts of different kinds”. Lists can contain any type of object, even other lists.

Here’s one more example:

Get your hands dirty in our next tutorial on list concatenation, multiplication, and slicing.

Life is Like A Python List was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • Enka

    I’m getting a little confused on what the order of the lessons is?
    Also I get an error when using randint:

    >>> i = randint(0, len(whizzo)-1)
    Traceback (most recent call last):
    File “”, line 1, in
    i = randint(0, len(whizzo)-1)
    NameError: name ‘randint’ is not defined

  • Hi Enka,
    Did you import randint from the random module? You will get an error if you don’t. You import modules like this:

    >>> from random import randint

    As for order of lessons, follow the link at the end of each lesson to take you to the next.

  • Kevin T

    Hey, I think you forgot to import the function “randint”:
    so before your line “i = randint(0, len(whizzo)-1)” type in “from random import randint” and press enter

  • Robert M.

    thanks for the tutorial, I hope to see the next one soon. This has been just the right amount of hand holding without making me feel like I’m just copying down the answers.

  • Arturo Flores

    I’ve checked out the python tutorials you’ve created. This was crucially helpful. THANKS!

  • AOShin

    Is the link to the next chapter missing or is there no next chapter? I just got into the flow of learning. I want more!

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid