Skip to content
Happy Programming Guide
Start learning
Programming Basics

Arrays and Lists Explained (With Simple Examples)

A list holds many values under one name, in order. Here is how to read, add, remove and loop over items — and why the first position is zero.

A list holds many values under one name, in a set order. Instead of three variables you have one list with three items.

Python
scores = [10, 8, 9]

Python calls it a list. JavaScript, Java and most other languages call the same idea an array. The word differs; the concept does not.

Why you want one#

Without a list, five students means five variables:

Python
student1 = "Ada"
student2 = "Sam"
student3 = "Kai"   # this does not scale

With a list, five or five thousand costs the same effort:

Python
students = ["Ada", "Sam", "Kai"]
print(len(students))   # 3

Getting items out: positions start at zero#

Python
students = ["Ada", "Sam", "Kai"]

print(students[0])    # Ada
print(students[1])    # Sam
print(students[2])    # Kai
print(students[-1])   # Kai  (Python counts backwards too)

The first item is at position 0. This catches everyone once. A useful way to think about it: the position is “how many items to skip”, so the first item skips none.

Asking for a position that does not exist raises an error:

Python
print(students[3])   # IndexError: list index out of range

JavaScript is quieter about it and gives you undefined, which then breaks something further down. See common JavaScript errors.

Changing a list#

Python
students = ["Ada", "Sam"]

students.append("Kai")       # add to the end
students.insert(0, "Zara")   # add at a position
students.remove("Sam")       # remove by value
students[0] = "Zaynab"       # replace by position

print(students)   # ['Zaynab', 'Ada', 'Kai']

JavaScript equivalents:

JavaScript
const students = ["Ada", "Sam"];

students.push("Kai");        // add to the end
students.unshift("Zara");    // add to the start
students[0] = "Zaynab";      // replace by position

console.log(students.length);

Looping over a list#

This is what lists are really for. See loops explained for the full picture.

Python
prices = [120, 80, 200]
total = 0

for price in prices:
    total = total + price

print("Total:", total)   # 400

Useful things you can ask a list#

Python
numbers = [4, 9, 2, 7]

print(len(numbers))         # 4   how many
print(max(numbers))         # 9   biggest
print(min(numbers))         # 2   smallest
print(sum(numbers))         # 22  added up
print(sorted(numbers))      # [2, 4, 7, 9]
print(2 in numbers)         # True — is it there?

Lists versus dictionaries#

Use a list when order matters and items are the same kind of thing. Use a dictionary when each value needs a label.

Python
scores = [90, 75, 60]                          # a list
person = {"name": "Ada", "score": 90}          # a dictionary

More in Python dictionaries explained.

Questions people ask#

What is the difference between a list and an array?

In everyday use, nothing — they are the same idea with different names. Python does have a separate array type for numeric data, but beginners almost never need it.

Can a list hold different types?

In Python and JavaScript, yes: [1, "two", True] is legal. It is usually a sign something is off, though. Lists are easiest to work with when every item is the same kind of thing.

How do I get part of a list?

Python slices with numbers[1:3], which gives items at positions 1 and 2 — the end position is not included. JavaScript uses numbers.slice(1, 3) with the same rule.

What is a tuple?

A Python list that cannot be changed after it is created, written with round brackets: (3, 4). Useful for fixed things like coordinates, where accidental edits would be a bug.

Where to go next#

Next lessonWhat is an algorithm?

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *