Lists in Python
Subject: Data Structures and Algorithms · Language: Python · Level: beginner · 7 min read
Learn Python lists, indexing, traversal, input, updating values, sum, max, min, sorting, and common DSA list patterns.
A list is an ordered collection of values. It is one of the most important Python data structures for DSA because it works like a dynamic array. A list can store many values under one name and each value can be accessed using an index.
Python lists use zero-based indexing. The first element is at index `0`, the second is at index `1`, and the last is at index `len(arr) - 1`. Understanding indexes is essential because most list problems depend on correct position handling.
Lists are mutable, so their contents can be changed. You can update an element, append new values, remove values, reverse the list, sort it, and traverse it using loops. This flexibility makes lists useful for many coding problems.
Common list problems include finding sum, maximum, minimum, count, search, reverse, sorting, frequency, prefix sum, two pointers, and sliding window. These patterns appear repeatedly in placement coding rounds.
Students should be careful while accessing indexes. If a list has `n` elements, accessing index `n` is invalid because the last valid index is `n - 1`. Python also supports negative indexing, where `-1` means the last element, but beginners should use it carefully.
A strong learning habit is to solve list problems first with normal loops. After understanding the logic, learn Python shortcuts like `sum(arr)`, `max(arr)`, `min(arr)`, slicing, and list comprehensions. Shortcuts are useful only when the concept is already clear.
A list is a data structure that stores multiple values in a single variable. Lists are one of the most important Python topics for DSA because they work like dynamic arrays.
For example, if you want to store marks of 50 students, creating 50 separate variables is not practical. A list lets you store all marks under one name and process them using loops.
Python lists are ordered, mutable, and zero-indexed. Ordered means values stay in sequence. Mutable means values can be changed after creation. Zero-indexed means the first value is at index `0`.
Why Lists Are Needed
Lists help us manage a group of related values. Instead of writing `mark1`, `mark2`, `mark3`, and so on, we can write one list named `marks`.
Once values are stored in a list, the same loop can calculate sum, find maximum, count values, search for a target, reverse values, or sort the list.
Creating a List
A list is created using square brackets.
```python
numbers = [10, 20, 30, 40, 50]
```
Here, `numbers` stores five integer values. Python lists can technically store mixed types, but for DSA you usually store values of the same type together.
Empty List
An empty list can be created when values will be added later.
```python
arr = []
arr.append(10)
arr.append(20)
```
The `append()` method adds a value at the end of the list.
List Indexing
Python lists use zero-based indexing. The first element is at index `0`, and the last element is at index `len(arr) - 1`.
```python
arr = [10, 20, 30, 40, 50]
print(arr[0])
print(arr[4])
```
Output
```text
10
50
```
`arr[0]` prints the first value, and `arr[4]` prints the last value in this list.
Negative Indexing
Python also supports negative indexing. `arr[-1]` gives the last element, `arr[-2]` gives the second-last element, and so on.
```python
arr = [10, 20, 30]
print(arr[-1])
```
Output
```text
30
```
Negative indexing is convenient, but use it carefully in DSA so your logic remains clear.
List Length
Use `len(arr)` to find how many elements are present in a list.
```python
arr = [5, 10, 15]
print(len(arr))
```
Output
```text
3
```
This prints `3`. The last valid index is `len(arr) - 1`.
Updating List Elements
List values can be changed using their index.
```python
arr = [10, 20, 30]
arr[1] = 99
```
After this, the list becomes `[10, 99, 30]`.
Taking List Input
In DSA problems, list input is often given in one line. Use `split()` and `map()` to read it.
```python
arr = list(map(int, input().split()))
```
If input is `12 45 7 89 23`, this creates a list of integers.
Sometimes the size is given first.
```python
n = int(input())
arr = list(map(int, input().split()))
```
Here, `n` tells how many values are expected. In real coding, you can also check whether `len(arr) == n` when validation matters.
Traversing a List
Traversal means visiting each element one by one. You can traverse by value or by index.
```python
for value in arr:
print(value)
```
Sample Output when `arr = [10, 20, 30]`
```text
10
20
30
```
Use this when you only need values.
```python
for i in range(len(arr)):
print(i, arr[i])
```
Sample Output when `arr = [10, 20, 30]`
```text
0 10
1 20
2 30
```
Use this when you need indexes, positions, or updates.
Common List Operations
- Sum: add all values in the list
- Maximum: find the largest value
- Minimum: find the smallest value
- Count: count values that satisfy a condition
- Search: check whether a target exists
- Reverse: process values in opposite order
- Sort: arrange values in increasing or decreasing order
Python provides built-in functions like `sum(arr)`, `max(arr)`, and `min(arr)`. These are useful, but students should also understand how to calculate them using loops.
Sorting a List
Use `arr.sort()` to sort the original list. Use `sorted(arr)` to create a new sorted list.
```python
arr = [4, 1, 9, 2]
arr.sort()
```
After sorting, `arr` becomes `[1, 2, 4, 9]`.
Slicing a List
Slicing is used to take a part of a list.
```python
arr = [10, 20, 30, 40, 50]
print(arr[1:4])
```
Output
```text
[20, 30, 40]
```
This prints values from index 1 to index 3. The ending index is not included.
Common DSA Patterns with Lists
Lists are used to build many DSA patterns.
- Prefix sum is used for range sum problems
- Two pointers are used when processing from both ends
- Sliding window is used for subarray problems
- Frequency arrays count values or characters
- Sorting helps in searching, duplicate handling, and pair problems
Example Program
```python
n = int(input("Enter number of elements: "))
arr = list(map(int, input("Enter elements: " ).split()))
print("Sum =", sum(arr))
print("Maximum =", max(arr))
print("Minimum =", min(arr))
```
Sample Input
```text
5
12 45 7 89 23
```
Output
```text
Enter number of elements: Enter elements: Sum = 176
Maximum = 89
Minimum = 7
```
This program reads a list and prints the sum, maximum value, and minimum value.
Lists and Memory
Python lists store references to objects. When a list is passed to a function, the function can modify the original list if it changes list elements.
This is important in DSA because helper functions may update arrays or lists directly.
A list is one of the most important Python data structures for DSA. It stores multiple values in order and allows access using indexes. The first element is at index `0`, and the last element is at index `len(list) - 1`.
When solving list problems, first identify what kind of movement is needed. If you need every value, use a normal traversal. If you need positions, use `range(len(arr))` or `enumerate(arr)`. If you need two sides, use two pointers.
In interviews, using Python list methods is allowed in many problems, but students should still understand the logic behind them. For learning, write the loop manually first, then learn the shorter Python method.
Practice Question
Take `n` numbers as input and print how many numbers are even and how many are odd.