Lists
Store and manage multiple values in a single variable using Python lists.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1. What Are Lists?
A list is a collection that stores multiple items in a single variable. Lists are ordered , changeable , and allow duplicate values .
Key Characteristics
- Ordered — Items have a defined order (index 0, 1, 2...)
- Mutable — You can add, remove, or change items
- Allow duplicates — Same value can appear multiple times
- Mixed types — Can hold strings, numbers, booleans, etc.
2. Creating Lists
Type
Example
Description
Empty list
my_list = []
Start with nothing
Strings
["a", "b", "c"]
List of text values
Numbers
[1, 2, 3, 4, 5]
List of integers
Mixed
[1, "hello", True]
Different types together
Nested
[[1, 2], [3, 4]]
Lists inside lists
3. Accessing Items by Index
Each item in a list has an index (position number). Python uses zero-based indexing — the first item is at index 0.
Code
Result
Explanation
fruits[0]
"apple"
First item
fruits[2]
"cherry"
Third item
fruits[-1]
"date"
Last item (negative index)
fruits[-2]
Second to last
4. Changing List Items
Lists are mutable — you can change items after creation by assigning a new value to a specific index.
5. Adding Items to Lists
Method
What It Does
.append(item)
Add item to the end
fruits.append("mango")
.insert(i, item)
Add item at position i
fruits.insert(1, "pear")
.extend(list)
Add all items from another list
fruits.extend(["kiwi", "lime"])
6. Removing Items from Lists
.remove(item)
Remove first match by value
fruits.remove("banana")
.pop()
Remove and return last item
last = fruits.pop()
.pop(i)
Remove and return item at index i
first = fruits.pop(0)
.clear()
Remove all items
fruits.clear()
del list[i]
Delete item at index i
del fruits[0]
7. List Slicing — Get a Range of Items
Slicing lets you get a portion of a list using the syntax list[start:end] .
Meaning
nums[1:4]
[1, 2, 3]
Index 1 to 3 (4 excluded)
nums[:3]
[0, 1, 2]
First 3 items
nums[3:]
[3, 4, 5]
From index 3 to end
nums[-2:]
[4, 5]
Last 2 items
nums[::2]
[0, 2, 4]
Every 2nd item
nums[::-1]
[5, 4, 3, 2, 1, 0]
Reversed list
8. Looping Through Lists
Use When
for item in list:
You just need the items
for fruit in fruits:
for i, item in enumerate(list):
You need index + item
for i, fruit in enumerate(fruits):
9. Useful List Methods
Method/Function
Returns
len(list)
Number of items
Integer
.sort()
Sort ascending (modifies list)
None
.sort(reverse=True)
Sort descending
.reverse()
Reverse the order
.count(x)
Count occurrences of x
.index(x)
Find position of x
.copy()
Create a copy
New list
sum(list)
Sum of numbers
Number
min(list) / max(list)
Smallest / largest value
Item
10. Checking if Item Exists
11. List Comprehension (Advanced)
List comprehension is a concise way to create lists in one line. It is more advanced but very powerful once you learn it.
12. Common Mistakes to Avoid
Mistake
Problem
Solution
Index out of range
list[10]
Check len(list) first
Forgetting 0-indexing
Expecting first item at 1
First item is at index 0
Removing nonexistent item
.remove("x")
Check if x in list: first
Aliasing instead of copying
list2 = list1
Use list2 = list1.copy()
Using parentheses
(1, 2, 3)
Use square brackets [1, 2, 3]
13. Practical Examples
Example 1: Shopping Cart
Example 2: Grade Calculator
Example 3: To-Do List
Summary: Quick Reference
Operation
Syntax
Create
[item1, item2]
fruits = ["apple", "banana"]
Access
list[index]
Change
list[i] = new
fruits[0] = "apricot"
Add
fruits.append("cherry")
Remove
Slice
list[start:end]
fruits[1:3]
Length
len(fruits)
Loop
Check exists
item in list
"apple" in fruits
Lesson 7 complete — you've unlocked Python's most-used data structure!
Lists are everywhere in Python — shopping carts, user records, game inventories, search results. You can now create, access, modify, slice, sort, and loop through them.
🚀 Up next: Dictionaries — store data with named keys instead of position numbers, perfect for structured records like user profiles.
Practice quiz
Which brackets are used to create a list?
- { }
Lists use square brackets, e.g. [1, 2, 3].
For fruits = ['apple', 'banana', 'cherry'], what is fruits[0]?
- 'banana'
- 'apple'
- 'cherry'
- Error
Answer: 'apple'. Python uses zero-based indexing, so index 0 is the first item, 'apple'.
What does fruits[-1] return?
- The first item
- The last item
- An IndexError
- The second item
Answer: The last item. Negative indexing counts from the end; -1 is the last item.
Which method adds an item to the END of a list?
- .insert()
- .append()
- .extend()
- .add()
Answer: .append(). .append(item) adds a single item to the end of the list.
What does nums[1:4] return for nums = [0, 1, 2, 3, 4, 5]?
Slicing is start-inclusive, end-exclusive: indices 1, 2, 3 give [1, 2, 3].
What does nums[::-1] produce?
- The list reversed
- Every 2nd item
- The last item
- An empty list
Answer: The list reversed. A step of -1 reverses the list.
Accessing an index that doesn't exist raises which error?
- KeyError
- ValueError
- IndexError
- TypeError
Answer: IndexError. Using an out-of-range index raises an IndexError.
What does .remove() do if the item is NOT in the list?
- Returns None
- Does nothing
- Raises a ValueError
- Adds the item
Answer: Raises a ValueError. .remove() raises a ValueError when the value isn't found.
What does sum([10, 20, 30, 40]) return?
- 100
- 4
- 40
- 25
Answer: 100. sum() adds all the numbers: 10+20+30+40 = 100.
What does [x for x in range(10) if x % 2 == 0] produce?
The comprehension keeps even numbers from 0 to 9: [0, 2, 4, 6, 8].
Continue this course
- Previous: Functions
- Next: Dictionaries