Lists
In the previous module we used range() to loop over sequences of numbers. But what if you need to store a collection of values, work with them, modify them, or pass them around your program? That's what lists are for.
What is a List?
A list is an ordered collection of values stored under a single variable name. You can think of it as a row of boxes — each box holds a value and has a numbered position.
Python lists:
- Can store elements of mixed types (though in practice you usually keep one type)
- Have no fixed size — they grow and shrink dynamically
- Come with a rich set of built-in methods
Creating a List
Indexing
Each element has an index — its position in the list. Indices start at 0:
Negative Indices
Python also supports negative indices — they count from the end of the list:
This is very convenient — numbers[-1] always gives you the last element without needing to know the length.
Modifying Elements
You can assign a new value to any index:
Index Out of Range
Accessing an index that doesn't exist raises an IndexError:
Always make sure your index is between 0 and len(numbers) - 1.
Length of a List
Function len() returns the number of elements:
This is especially useful in loops:
Slicing
Slicing lets you extract a portion of a list. The syntax is list[start:stop] — returns elements from index start up to but not including stop:
Omitting Start or Stop
If you omit start, it defaults to 0. If you omit stop, it defaults to the end of the list:
Slice with Step
Just like range(), slices support a step:
numbers[::-1] is the most common Python idiom for reversing a list.
Iterating Over a List
Simple iteration — values only
This is the cleanest way when you only need the values.
Iteration with index using range(len(...))
Use this when you need the index to modify elements or reference their position.
Iteration with enumerate()
enumerate() gives you both the index and the value at the same time:
Output:
You can also set a custom starting index:
In competitive programming, input is usually read from stdin. Common patterns:
Note: The list(map(...)) pattern uses concepts that will be covered in more detail in a later module. For now, treat it as a standard recipe for reading a list of numbers on one line.
List Methods
Lists come with many built-in methods. Here are the ones you will use most often.
append(x) — add to the end
insert(i, x) — insert at position
pop() — remove and return last element
You can also pop by index:
remove(x) — remove first occurrence of value
sort() — sort in place
sort() modifies the list directly. If you need a sorted copy without changing the original, use sorted():
reverse() — reverse in place
index(x) — find index of first occurrence
count(x) — count occurrences
extend(other) — append all elements of another list
Alternatively, you can use + to concatenate two lists into a new one:
clear() — remove all elements
Checking if an Element Exists
Use the in operator:
Note that in on a list checks every element one by one — it is O(n). For large lists where you need fast lookups, use a set instead (covered in a later module).
Common List Operations in CP
Finding minimum and maximum
Sum of all elements
Creating a list filled with a default value
This is the standard way to initialize a list of a given size in Python.
Printing a List
By default, print(a) shows the list with brackets and commas:
In competitive programming, output usually requires elements separated by spaces with no brackets. Use the * (unpack) operator for this:
*a unpacks the list — it passes each element as a separate argument to print(), which then separates them with spaces by default. You can control the separator:
Practical Examples
Sum of elements
Finding the minimum and its index
Counting a specific value
Copying a list in reverse order
Checking if a list is sorted
Summary of Key Methods
| Method / Operation | Description |
|---|
a[i] | Access element at index i |
a[i] = x | Modify element at index i |
a[i:j] | Slice from i to j (exclusive) |
len(a) | Number of elements |
a.append(x) | Add x to the end |
a.insert(i, x) | Insert x at index i |
a.pop() | Remove and return last element |
a.remove(x) | Remove first occurrence of x |
a.sort() | Sort in place |
|