Programming · StudentHub Lesson
Lists & Arrays
Learn how to store and manipulate collections of data using lists in Python.
What you will learn
- Create and access elements in a Python list
- Use indexing to retrieve list items
- Add and remove items from a list
- Loop through a list using a for loop
- Understand list length and common list methods
Watch the lesson
Python for Beginners – Full Course [Programming Tutorial] · freeCodeCamp.org
Watch on YouTubeTopic notes
Main Idea
Lists (arrays) let programs store multiple values together in a single ordered collection.
Key Concepts
- Lists are created with square brackets
- Items are accessed using zero-based indexing
- Lists are mutable: you can add, remove, or change elements
- Loops are commonly used to process every item in a list
Definitions
- List: an ordered, mutable collection of items
- Index: the position of an item in a list, starting at 0
- Method: a built-in function associated with a data type, like append()
Syntax
```python
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
fruits.append("date") # add item
fruits.remove("banana") # remove item
for fruit in fruits:
print(fruit)
```
Examples
len(fruits)returns the number of items in the listfruits[-1]accesses the last item in the list
Common Mistakes
- Forgetting that indexing starts at 0, not 1
- Trying to access an index that doesn't exist (IndexError)
Key concepts
Important terms
- List
- An ordered, changeable collection of items in Python
- Index
- The numeric position of an item in a list, starting at 0
Worked examples
Problem
Access the second item in the list [10, 20, 30]
- 1. Index 1 refers to the second item
- 2. numbers[1]
Answer: 20
Quick revision
- Lists store multiple values in order
- Indexing starts at 0
- append() adds items, remove() deletes them
- len() gives the number of items
- for loops iterate through list items
Check your understanding
Question 1 · Multiple choice
What index refers to the first item in a Python list?
Question 2 · Multiple choice
Which method adds an item to the end of a list?
Question 3 · True or false
Lists in Python can contain items of different data types.
Question 4 · Short answer
What does len([1,2,3]) return?
Question 5 · Short answer
How would you access the last item of a list called data?
Done with Lists & Arrays?
Sign in to save your progress across the library.