Every variable you’ve used so far has held exactly one value. One number, one word, one True/False. But real programs rarely deal with just one thing, you’ve got a class full of students, a week of temperatures, a cart full of items. Storing each of those in a separate variable would be painful. That’s the problem a list solves.
What a list actually is?
A list is a single variable that holds multiple values, in a specific order, and you can add, remove, or change items whenever you want. You create one with square brackets, separating each item with a comma.
fruits = ["apple", "banana", "mango"]
print(fruits)Output: ['apple', 'banana', 'mango']A list can hold numbers, strings, even a mix of different types in the same list, though in practice you’ll mostly keep one type per list to stay sane.
numbers = [10, 25, 3, 47]
mixed = ["Nepal", 2026, True]Accessing items with indexing
Every item in a list has a position, called an index, starting at 0, not 1. This trips up almost everyone at first.
fruits = ["apple", "banana", "mango"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # mangoPython also lets you count backward from the end using negative indexes.
print(fruits[-1]) # mango, the last item
print(fruits[-2]) # banana, second from the endChanging a value in a list
Unlike strings, lists are mutable, meaning you can change an item after creating the list, without having to build a new one.
fruits = ["apple", "banana", "mango"]
fruits[1] = "orange"
print(fruits) # ['apple', 'orange', 'mango']Adding items: append() and insert()
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits) # ['apple', 'banana', 'mango']append() always adds to the end. If you need to add something at a specific position instead, use insert().
fruits.insert(1, "kiwi")
print(fruits) # ['apple', 'kiwi', 'banana', 'mango']Removing items: remove(), pop(), and clear()
fruits = ["apple", "banana", "mango"]
fruits.remove("banana")
print(fruits) # ['apple', 'mango']remove() deletes by value, not position. If the value doesn’t exist in the list, Python throws an error, so it’s worth checking first if you’re not sure it’s there.
fruits = ["apple", "banana", "mango"]
last_item = fruits.pop()
print(last_item) # mango
print(fruits) # ['apple', 'banana']pop() removes the last item by default, and hands it back to you, which is genuinely useful when you need the removed value for something else. Give it a position, like fruits.pop(0), and it removes that specific item instead.
Looping through a list
You already saw this in the for loop lesson, but it’s worth repeating here since it’s the most common thing you’ll do with a list.
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)Checking length and membership
fruits = ["apple", "banana", "mango"]
print(len(fruits)) # 3
print("banana" in fruits) # True
print("grape" in fruits) # Falselen() tells you how many items are in the list. in Checks whether a value exists inside it, same membership operator you already learned, it just works on lists too, not only strings.
Sorting a list
numbers = [5, 2, 8, 1]
numbers.sort()
print(numbers) # [1, 2, 5, 8]
numbers.sort(reverse=True)
print(numbers) # [8, 5, 2, 1]sort() changes the original list permanently. If you want a sorted copy while keeping the original untouched, use sorted(numbers) instead.
Python List Practice Questions
1. Create a list of 5 of your favorite movies and print the second item.
2. Write code that adds a new item to the end of an existing list using append().
3. What is the output of the following code?
numbers = [4, 8, 15, 16]
print(numbers[-1])
print(numbers[-3])4. Write a program that removes the value "mango" from a list, but only if it actually exists in the list. (Hint: use the in operator first.)
5. What’s the difference in output between numbers.sort() and sorted(numbers)? Write both and compare.
6. Write a for loop that prints only the even numbers from this list: [3, 8, 11, 20, 5, 14].
