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.
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:
snacksis 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
[], andlen([])is0. You can start empty andappenditems one by one. appendadds 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
arraymodule, 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.
Sources
- Python Software Foundation, "An Informal Introduction to Python": Lists: a list is written as items between square brackets, separated by commas; len() counts them; append() adds to the end
- Python Software Foundation, "Data Structures": More on Lists: list.append adds an item to the end
- Python Software Foundation, "Built-in Functions": len: returns the number of items
- MDN Web Docs, Array: JavaScript calls its ordered list of values an array
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"
- "Intro to Programming" EP11Infinite loops
- "Intro to Programming" EP12Functions
- "Intro to Programming" EP16Reading an error message
- "Intro to Programming" EP17Syntax vs logic errors