A list holds many values under one name, in a set order. Instead of three variables you have one list with three items.
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:
student1 = "Ada"
student2 = "Sam"
student3 = "Kai" # this does not scaleWith a list, five or five thousand costs the same effort:
students = ["Ada", "Sam", "Kai"]
print(len(students)) # 3Getting items out: positions start at zero#
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:
print(students[3]) # IndexError: list index out of rangeJavaScript is quieter about it and gives you undefined, which then breaks something further down. See common JavaScript errors.
Changing a list#
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:
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.
prices = [120, 80, 200]
total = 0
for price in prices:
total = total + price
print("Total:", total) # 400Useful things you can ask a list#
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.
scores = [90, 75, 60] # a list
person = {"name": "Ada", "score": 90} # a dictionaryMore 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.