What a List Is, and How to Index It
Quick answer A list is an ordered, mutable, mixed-type collection written in square brackets, whose items are reached by index numbers that start at 0 forwards and -1 backwards.
A list is Python's way of holding many values under one name, in order. Without lists you would write m1 = 88, m2 = 92, m3 = 76 — and then be stuck the moment a fourth student joins the class. With a list, the count stops mattering.
You write a list inside square brackets, items separated by commas. Two things surprise students who have seen arrays in C++ or Java: a Python list does not require all items to be of the same type, and it has no fixed size.
Worked example 1 — creating lists
marks = [88, 92, 76, 95, 61]
names = ['Aarav', 'Diya', 'Kabir']
mixed = [101, 'Aarav', 88.5, True]
empty = []
print(marks)
print(names)
print(mixed)
print(empty, len(empty))
print(len(marks), len(mixed))
print(type(marks))
print(list('IRCTC'))
print(list(range(1, 6)))
print(list((10, 20, 30)))
Output
[88, 92, 76, 95, 61]
['Aarav', 'Diya', 'Kabir']
[101, 'Aarav', 88.5, True]
[] 0
5 4
['I', 'R', 'C', 'T', 'C']
[1, 2, 3, 4, 5]
[10, 20, 30]
Three things to take from that output. [] is a perfectly valid empty list of length 0. len(mixed) is 4 — len() counts items, never the characters inside them. And list() is a built-in type conversion: hand it a string and you get a list of characters, hand it a range and you get the numbers, hand it a tuple and you get the same items as a list. list() with nothing inside gives [].
Indexing
Every item sits at a numbered position called its index. Counting starts at 0, not 1. The reason is worth knowing: an index is really a distance from the start. The first item is 0 steps away from the beginning, the second is 1 step away. Python also allows negative indexes, counted from the right end.
| Item | 'Aarav' | 'Diya' | 'Kabir' | 'Meera' | 'Rohan' |
|---|---|---|---|---|---|
| Positive index | 0 | 1 | 2 | 3 | 4 |
| Negative index | -5 | -4 | -3 | -2 | -1 |
Worked example 2 — indexing forwards and backwards
students = ['Aarav', 'Diya', 'Kabir', 'Meera', 'Rohan']
print(students[0])
print(students[3])
print(students[-1])
print(students[-5])
print(len(students))
print(students[len(students) - 1])
Output
Aarav
Meera
Rohan
Aarav
5
Rohan
Now the error you must be able to name in the exam. A 5-item list has valid indexes 0 to 4, so asking for index 5 stops the program:
students = ['Aarav', 'Diya', 'Kabir', 'Meera', 'Rohan']
print(students[5])
Error output
IndexError: list index out of range
That is why students[len(students)] is always wrong and students[len(students) - 1] is always the last item. Negative indexes exist precisely so you never have to write that subtraction: students[-1] is shorter and cannot be got wrong.
Worked example 3 — lists are mutable, strings are not
marks = [88, 92, 76]
print('before:', marks)
marks[1] = 97
print('after :', marks)
marks[-1] = 80
print('after :', marks)
Output
before: [88, 92, 76]
after : [88, 97, 76]
after : [88, 97, 80]
marks[1] = 97 changes the list itself — no new list is made. Try the same instruction on a string and Python refuses:
name = 'Aarav'
name[0] = 'B'
Error output
TypeError: 'str' object does not support item assignment
A string refuses because strings are immutable. This one difference explains most of the trick questions later in the chapter, so fix it in your head now: a list can be edited in place; a string cannot.
- A list is an ordered, mutable collection in square brackets; items may be of different types and the size grows and shrinks freely.
- Indexes run 0 to len(L)-1 forwards and -1 to -len(L) backwards; anything outside that raises IndexError.
- len(L) counts top-level items only, and list() converts a string, range or tuple into a list (list() alone gives []).
- L[i] = value works because lists are mutable; the same statement on a string raises TypeError: 'str' object does not support item assignment.
