A list is one of the most versatile and frequently used data structures in Python. It is an ordered, mutable collection of items that can hold elements of different data types, including numbers, strings, and even other lists. Because lists are mutable, we can add, remove, and modify their elements after the list has been created, which makes them ideal for storing collections of data that change during a program's execution.
A list is created by placing comma-separated values inside square brackets [ ]. Unlike arrays in other languages, Python lists are heterogeneous, meaning a single list can contain an integer, a float, a string, and a boolean at the same time. Lists support indexing, slicing, concatenation, repetition, and a rich collection of built-in methods that make data manipulation concise and expressive.
This chapter covers everything a student needs for list manipulation: creating and accessing lists, traversing them with loops, understanding the difference between list methods and built-in functions, and learning how to sort, reverse, count, and modify elements. We will also look at nested lists and list comprehensions, which are elegant Python features that appear in many competitive questions. A clear understanding of lists is essential before moving on to dictionaries and data visualisation, since lists are used to hold data in almost every practical program.
A list is created using square brackets with items separated by commas.
marks = [67, 89, 54, 78]
names = ["Amit", "Bina", "Chirag"]
mixed = [10, "hello", 3.5, True]
empty = []
We can also create a list using the list() constructor and the range() function:
numbers = list(range(1, 6))
print(numbers)
Output:
[1, 2, 3, 4, 5]
Each element of a list is accessed using an index inside square brackets. Indexing starts from 0, and negative indices count from the end, where -1 refers to the last element.
marks = [67, 89, 54, 78]
print(marks[0])
print(marks[2])
print(marks[-1])
Output:
67
54
78
Slicing extracts a portion of the list. list[start:stop:step] returns a new list containing elements from start up to but not including stop.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
print(numbers[:3])
print(numbers[::2])
Output:
[20, 30, 40]
[10, 20, 30]
[10, 30, 50]
The for loop can traverse a list by index or directly by element.
marks = [67, 89, 54, 78]
for m in marks:
print(m)
for i in range(len(marks)):
print(i, marks[i])
The len() function returns the number of elements in the list, and enumerate() can also provide both index and value:
for i, m in enumerate(marks):
print(i, m)
Python provides many built-in methods for lists:
append(item): Adds an item at the end.extend(iterable): Adds all elements of an iterable at the end.insert(index, item): Inserts an item at a given position.remove(item): Removes the first occurrence of the item.pop(index): Removes and returns the item at the index (last item by default).index(item): Returns the index of the first occurrence of the item.count(item): Returns the number of occurrences of the item.sort(): Sorts the list in ascending order in place.reverse(): Reverses the order of the list in place.clear(): Removes all items from the list.fruits = ["apple", "banana", "mango"]
fruits.append("orange")
print(fruits)
fruits.insert(1, "grapes")
print(fruits)
fruits.sort()
print(fruits)
fruits.pop()
print(fruits)
Output:
['apple', 'banana', 'mango', 'orange']
['apple', 'grapes', 'banana', 'mango', 'orange']
['apple', 'banana', 'grapes', 'mango', 'orange']
['apple', 'banana', 'grapes', 'mango']
The following built-in functions work with lists:
len(list): Number of elements.max(list): Maximum element.min(list): Minimum element.sum(list): Sum of all elements.sorted(list): Returns a new sorted list without changing the original.reversed(list): Returns a reversed iterator.marks = [67, 89, 54, 78]
print(max(marks))
print(min(marks))
print(sum(marks))
print(sorted(marks, reverse=True))
Output:
89
54
288
[89, 78, 67, 54]
Note the difference: sort() modifies the original list, while sorted() returns a new list and leaves the original unchanged.
list1 + list2 joins two lists.list * n repeats the list n times.item in list checks membership and returns True or False.item not in list checks that the item is absent.a = [1, 2]
b = [3, 4]
print(a + b)
print(a * 3)
print(2 in a)
Output:
[1, 2, 3, 4]
[1, 2, 1, 2, 1, 2]
True
A list can contain other lists, creating a nested (two-dimensional) structure.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[0])
print(matrix[1][2])
Output:
[1, 2, 3]
6
Here matrix[1][2] first selects the second row [4, 5, 6] and then the third element 6.
List comprehension provides a concise way to create lists. Its basic syntax is [expression for item in iterable if condition].
squares = [x * x for x in range(1, 6)]
print(squares)
evens = [x for x in range(1, 11) if x % 2 == 0]
print(evens)
Output:
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
Lists appear in almost every real Python program because they can store any collection of items whose size changes while the program runs. A common application is storing the marks of a class and then computing statistics. Once the marks are in a list, the built-in functions give the answers immediately: max() gives the highest mark, min() the lowest, sum() the total, and len() the number of students, so the average is simply sum(marks) / len(marks). Sorting the list either with the sort() method or the sorted() function makes it easy to identify the toppers, and slicing can extract the top three from the sorted list. Another everyday use is maintaining a shopping list, where append() adds new items, remove() drops items that are no longer needed, and the in operator checks whether an item is already present before it is added again.
Membership testing is one of the most valuable list features. The expression item in list returns True when the item appears at least once, which lets a program decide what to do next. For example, a library program can check whether a requested book is available in a list of issued books, and an attendance program can verify whether a roll number belongs to a list of present students. Such checks, combined with loops and conditionals, show how lists integrate naturally with the rest of the language. Because list operations are so convenient, the majority of the programs in the practical examination can be solved by choosing the right method and using it correctly, so building a mental catalogue of what each method does is time well spent.
| Method | Action | Example |
|---|---|---|
| append(x) | Add x at end | [1,2].append(3) -> [1,2,3] |
| insert(i,x) | Insert x at index i | [1,3].insert(1,2) -> [1,2,3] |
| remove(x) | Remove first x | [1,2,1].remove(1) -> [2,1] |
| pop(i) | Remove and return item at i | [1,2,3].pop(0) -> 1 |
| sort() | Sort in place ascending | [3,1,2].sort() -> [1,2,3] |
| reverse() | Reverse in place | [1,2].reverse() -> [2,1] |
| index(x) | First index of x | [1,2].index(2) -> 1 |
| count(x) | Count occurrences of x | [1,1,2].count(1) -> 2 |
| Function | Type | Behaviour |
|---|---|---|
| len(l) | Built-in | Number of elements |
| max(l) | Built-in | Largest element |
| min(l) | Built-in | Smallest element |
| sum(l) | Built-in | Total of all elements |
| sorted(l) | Built-in | Returns new sorted list |
| l.sort() | Method | Sorts the original list in place |
| l.append(x) | Method | Modifies the list |
| l.pop() | Method | Modifies the list |
sort() and then assigning the result, since sort() returns None, not the sorted list.append() with extend(); append([1,2]) adds the list as one element, while extend([1,2]) adds each element separately.len(list) - 1.IndexError.pop() without an index and expecting the first element; by default it removes the last element.sort() with the built-in sorted() function.TypeError.pop(): it removes and returns the last element.list[-1] is the last element and list[-2] is the second last.append versus extend questions, as they are common in exams.sorted() works on any iterable while sort() works only on lists.sum() and len().Lists are the workhorse of Python data handling. Their mutability, heterogeneous nature, and rich set of methods make them perfect for storing and manipulating collections of data. Indexing, slicing, traversal, and methods like append, pop, sort, and reverse cover most everyday needs, while nested lists and comprehensions add power and elegance. The key distinctions between methods that modify lists in place and functions that return new lists must be memorised carefully. With lists mastered, the transition to dictionaries and data visualisation becomes smooth, as lists form the raw material that these higher-level tools consume.