Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 14 · Functions and lists

Lists and arrays

A list keeps many values together under one name. In Python you write it with square brackets and commas, len() counts its items, and append() adds a new item to the end. Many other languages call the same idea an array.

Posted · About a 1 minute watch, 4 minute read · Take the quiz

One name, many values

So far, every variable has held one value: one number, one piece of text. But real programs often need to keep track of lots of things: the songs in a playlist, the scores in a game, the items in a shopping basket.

You could make one variable for each:

``python snack1 = "apple" snack2 = "chips" snack3 = "grapes" ``

That works for three, but it gets messy fast. What if you have fifty? A list fixes this. It's like a shopping list: one name at the top, and lots of items underneath. Instead of three boxes, you get one box with three slots.

Making a list

``python snacks = ["apple", "chips", "grapes"] ``

Here's what each part does:

  • snacks is the list's name, just like any variable name.
  • The square brackets [ ] mark the start and end of the list.
  • Each item goes inside, separated by commas. These items are text, so each one is in quotes.

The items stay in the order you wrote them: apple first, then chips, then grapes.

Printing it and counting it

``python snacks = ["apple", "chips", "grapes"] print(snacks) print(len(snacks)) ``

`` ['apple', 'chips', 'grapes'] 3 ``

print(snacks) shows the whole list, brackets and all. Notice that Python prints the text with single quotes, even though we typed double quotes. That's just how Python displays lists; both kinds of quotes mean the same thing.

len() is short for length. It counts how many items are in the list. Three snacks, so it prints 3.

Adding an item with append

Lists can grow. .append() adds a new item to the end of the list:

``python snacks = ["apple", "chips", "grapes"] snacks.append("nuts") print(snacks) ``

`` ['apple', 'chips', 'grapes', 'nuts'] ``

The dot in snacks.append means "do this to the snacks list". The new item, "nuts", goes inside the brackets, and it lands after "grapes", at the end.

Lists and arrays

Many other languages call this idea an array. JavaScript has arrays, and so do Java and C. Same idea: one name, many values, kept in order. If you see the word "array" in a tutorial for another language, think "list".

Try it yourself

Open any free online Python editor (search "online Python") and run this:

``python colors = ["red", "blue"] colors.append("green") print(colors) print(len(colors)) ``

You should see:

`` ['red', 'blue', 'green'] 3 ``

Now make a list of your own favourite foods, append one more, and print the list and its length. A list can even hold a mix of types:

``python mixed = [3, "hi", 2.5, True] print(mixed) ``

`` [3, 'hi', 2.5, True] ``

Good to know

  • Every snippet on this page was run with Python 3 and printed exactly what is shown.
  • An empty list is just [], and len([]) is 0. You can start empty and append items one by one.
  • append adds exactly one item each time, always at the end.
  • A Python list can hold any mix of types: numbers, text, True/False, even other lists.
  • Strictly speaking, arrays in Java and C have a fixed size and hold only one type of item, while a Python list can grow and mix types. Python also has a separate array module, but beginners almost always use lists.
  • Next episode: indexing starts at zero, how to get one item out of a list by its position.

Answer to the comment question

What does this print?

``python pets = ["cat", "dog"] pets.append("fish") pets.append("cat") print(len(pets)) ``

It prints 4.

`` 4 ``

The list starts with two items: "cat" and "dog". The first append adds "fish" (three items), and the second append adds "cat" (four items). The trap is thinking the second "cat" doesn't count because it's already there. But a list happily holds the same value twice, so len counts it as a fourth item.

Test your knowledge

Quick quiz: Lists and arrays

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What is a list?
2Which line makes a Python list correctly?
3What does len() do with a list?
4What does snacks.append("nuts") do?

Sources

Video transcript

How can one variable hold many values?

Use a list. It's like a shopping list: one name at the top, and lots of items underneath. Instead of three boxes, you get one.

In Python: snacks equals, then square brackets, with apple, chips and grapes in quotes, separated by commas.

Print snacks, and Python shows the whole list, brackets and all.

Len, short for length, counts the items inside: three snacks.

Snacks dot append, with nuts in brackets, adds a new item to the end. Print it again, and now there are four.

Many other languages call this an array. Same idea: one name, many values, kept in order.

What does this print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons