ShareYourGroupShareYourGroupPython

Lesson 11 of 21 · about 6 minutes with the exercises

Lists

A list holds many values under one name, in order. A class has many students. A bill has many items. A week has seven temperatures. Without lists you would need a separate variable for each one.

Making a list

Put the values in square brackets with commas between them.

marks = [78, 45, 91, 60]
names = ["Asha", "Ravi", "Meena"]
mixed = ["pen", 12, 4.5, True]
empty = []
print(marks)
print(len(names))
print(empty)

Output

[78, 45, 91, 60]
3
[]

A list can mix types, though most lists hold one kind of thing. len gives the number of items.

Reading items

Indexes work exactly as they do for strings. The first item is at 0, and negative numbers count from the end.

names = ["Asha", "Ravi", "Meena", "Kabir"]
print(names[0])
print(names[2])
print(names[-1])
print(names[1:3])

Output

Asha
Meena
Kabir
['Ravi', 'Meena']

Changing an item

Here lists differ from strings. A list is mutable: you can change it in place.

names = ["Asha", "Ravi", "Meena"]
names[1] = "Rohit"
print(names)

Output

['Asha', 'Rohit', 'Meena']

Adding items

append adds one item at the end. insert adds at a position you choose and pushes the rest along. extend adds all the items of another list.

cart = ["rice", "dal"]
cart.append("sugar")
print(cart)
cart.insert(0, "atta")
print(cart)
cart.extend(["tea", "salt"])
print(cart)

Output

['rice', 'dal', 'sugar']
['atta', 'rice', 'dal', 'sugar']
['atta', 'rice', 'dal', 'sugar', 'tea', 'salt']

These methods change the list itself and return nothing. So do not write cart = cart.append("sugar"). That replaces your list with None, a classic bug.

Removing items

cart = ["atta", "rice", "dal", "sugar", "tea"]
cart.remove("dal")
print(cart)
last = cart.pop()
print(last, cart)
first = cart.pop(0)
print(first, cart)
del cart[0]
print(cart)

Output

['atta', 'rice', 'sugar', 'tea']
tea ['atta', 'rice', 'sugar']
atta ['rice', 'sugar']
['sugar']

Removing a value that is not there is an error, so check first with in.

cart = ["rice", "tea"]
if "milk" in cart:
    cart.remove("milk")
else:
    print("No milk in the cart")

Output

No milk in the cart

Looping over a list

prices = [40, 120, 65]
for price in prices:
    print("Rs", price)
print("Total: Rs", sum(prices))
print("Dearest: Rs", max(prices))

Output

Rs 40
Rs 120
Rs 65
Total: Rs 225
Dearest: Rs 120

sum, max and min work on any list of numbers.

Building a list in a loop

Start with an empty list and append to it. This is one of the most used patterns in Python.

squares = []
for n in range(1, 6):
    squares.append(n * n)
print(squares)

Output

[1, 4, 9, 16, 25]

Two names, one list

This point trips up many learners. Assigning a list to a second name does not copy it. Both names point to the same list.

a = [1, 2, 3]
b = a
b.append(4)
print(a)

Output

[1, 2, 3, 4]

a changed too, because there is only one list. To get a real, separate copy, use the copy method.

a = [1, 2, 3]
b = a.copy()
b.append(4)
print(a)
print(b)

Output

[1, 2, 3]
[1, 2, 3, 4]

Lists inside lists

An item of a list can itself be a list. That gives you a table of rows. Use two indexes: the first picks the row, the second the item in it.

table = [
    ["Asha", 78],
    ["Ravi", 91],
]
print(table[1])
print(table[1][0])
for name, marks in table:
    print(name, "scored", marks)

Output

['Ravi', 91]
Ravi
Asha scored 78
Ravi scored 91

Try it yourself

Exercise 1. Make a list of five city names. Print the first, the last and how many there are.

Show one answer
cities = ["Delhi", "Mumbai", "Chennai", "Kolkata", "Jaipur"]
print(cities[0], cities[-1], len(cities))

Output

Delhi Jaipur 5

Exercise 2. Start with scores = [12, 45, 7, 30]. Add 50 at the end, remove the 7, and print the list and its total.

Show one answer
scores = [12, 45, 7, 30]
scores.append(50)
scores.remove(7)
print(scores, sum(scores))

Output

[12, 45, 30, 50] 137

Exercise 3. From numbers = [4, 9, 12, 7, 20, 3], build a new list holding only the even numbers.

Show one answer
numbers = [4, 9, 12, 7, 20, 3]
evens = []
for n in numbers:
    if n % 2 == 0:
        evens.append(n)
print(evens)

Output

[4, 12, 20]

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.