# Python Lists: Mutable Sequence Basics, Methods, and Examples

Source: https://devdocs10.com/articles/python/python-lists  
By Murat Bo · Published 2026-11-18 · DevDocs10 (https://devdocs10.com)

Python lists are ordered, mutable sequences that can store multiple values in one variable. Use a list when you need to add, remove, reorder, or update items after creation. This article shows how lists work, how to use them safely, and the mistakes that cause the most confusion.

> **Difficulty:** Beginner
>
> **Helpful to know first:** basic variables, how to write Python expressions, and how `for` loops work.

## How Python Lists Store and Change Items

A Python list is a container for a sequence of items in a fixed order. Each item has a position, called an index, and the first item is at index `0`. You can read items, replace items, add new items, and remove items.

Lists are mutable, which means the list object can change after you create it. That is the main reason lists are useful for real programs: you can keep updating the same collection instead of making a new one every time.

```python
colors = ["red", "green", "blue"]

print(colors[0])
print(colors[2])

colors[1] = "yellow"
print(colors)

# Output:
# red
# blue
# ['red', 'yellow', 'blue']
```

That example shows indexing and item replacement. The list keeps the same order, but one element changes in place.

## Working With Python Lists in Real Programs

### Building a list step by step

A common use of a list is collecting values as a program runs. For example, you might store names entered by a user or readings from a sensor.

```python
names = []
names.append("Ada")
names.append("Grace")
names.append("Linus")

print(names)
# Output: ['Ada', 'Grace', 'Linus']
```

Here the list starts empty and grows one item at a time. This is one of the most common list patterns in Python.

### Looping through items in order

Lists work naturally with `for` loops. Python gives you each item in turn, so you can process every value without using indexes unless you need them.

```python
temperatures = [18, 21, 19]

for temp in temperatures:
    print(temp + 1)

# Output:
# 19
# 22
# 20
```

Each loop pass receives one list item. The loop does not change the list unless your code assigns new values or calls a method that mutates it.

### Using indexes when position matters

Sometimes the position matters as much as the value. Lists let you read by index, and you can also use `len()` to find the last valid position.

```python
items = ["pen", "notebook", "eraser"]

print(len(items))
print(items[len(items) - 1])

# Output:
# 3
# eraser
```

This pattern is useful when you need the last item and do not want to hard-code an index.

### Copying and slicing a part of a list

Slicing creates a new list from a range of positions. That is useful when you want just part of the data, or when you need a separate list that starts with the same values.

```python
numbers = [10, 20, 30, 40]
middle = numbers[1:3]

print(middle)
print(numbers)

# Output:
# [20, 30]
# [10, 20, 30, 40]
```

The slice does not include the item at the end index. The original list stays unchanged because slicing builds a new list object.

### Storing mixed types when the program needs them

Python lists can hold values of different types. That flexibility is real, but use it with care. Most lists in good code contain one kind of data, because that makes the program easier to read and debug.

```python
record = ["Mina", 17, True]

print(record[0])
print(record[1])
print(record[2])

# Output:
# Mina
# 17
# True
```

This works, but the meaning of each position must be known by the programmer. For structured records, a dictionary or a small class is often clearer.

## How Python List Methods Change the List

Many list operations are methods that mutate the list in place. That means they change the existing object instead of returning a new list. This detail matters because some methods return `None`, and beginners often try to print the result of the method call itself.

### Adding items with append, extend, and insert

`append()` adds one item to the end. `extend()` adds several items from another iterable. `insert()` puts one item at a specific position.

```python
numbers = [1, 2]
numbers.append(3)
numbers.extend([4, 5])
numbers.insert(1, 10)

print(numbers)
# Output: [1, 10, 2, 3, 4, 5]
```

`append()` adds one object. `extend()` treats the argument as a sequence of items. That difference is easy to miss.

### Removing items with pop, remove, and clear

`pop()` removes and returns an item. `remove()` deletes the first matching value. `clear()` empties the entire list.

```python
fruits = ["apple", "banana", "cherry"]
last_fruit = fruits.pop()
fruits.remove("banana")

print(last_fruit)
print(fruits)

# Output:
# cherry
# ['apple']
```

`pop()` is useful when you need the removed value. `remove()` is useful when you know the value, but not the position.

### Sorting and reversing the same list

`sort()` and `reverse()` both change the list in place. If you need a new sorted list instead of changing the old one, use `sorted()`.

```python
scores = [8, 3, 10]
sorted_scores = sorted(scores)
scores.reverse()

print(sorted_scores)
print(scores)

# Output:
# [3, 8, 10]
# [10, 3, 8]
```

Here the list object changes only when `reverse()` runs. The `sorted()` call leaves the original list alone.

## How Python Lists Behave in Detail

Lists are ordered, indexable, and mutable. They can also grow and shrink automatically. Under the hood, Python stores list items in a way that makes reading by index fast and adding to the end efficient most of the time.

One common misunderstanding is that copying a list means copying everything inside it. That is not always true. A shallow copy duplicates the outer list, but nested mutable objects are still shared.

```python
original = [[1, 2], [3, 4]]
copy_list = original[:]
copy_list[0].append(99)

print(original)
print(copy_list)

# Output:
# [[1, 2, 99], [3, 4]]
# [[1, 2, 99], [3, 4]]
```

The outer list is different, but both lists still point to the same inner lists. If you need a deep copy of nested data, use `copy.deepcopy()` from the standard library `copy` module.

Another detail is that list methods usually modify the list and return `None`. This is why code like `result = numbers.sort()` does not work the way many beginners expect. The method does its job, but the variable receives `None`.

List comparison uses element-by-element comparison. Two lists are equal if they have the same length and matching items in the same order.

```python
print([1, 2] == [1, 2])
print([1, 2] == [2, 1])

# Output:
# True
# False
```

> A list can hold other lists, strings, numbers, booleans, or any Python object. The language does not force one element type, but your own code is usually easier to understand if each list has one clear purpose.

## Predict the Output

### Aliasing the same list twice

What does this print?

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

**Answer:**

Both variables refer to the same list object. The `append()` call changes that one object, so both names see the change.

```bash
# Output:
# [1, 2, 3]
# [1, 2, 3]
```

### Slicing versus direct reference

What happens here?

```python
x = [10, 20, 30]
y = x[:]
y[0] = 99
print(x)
print(y)
```

**Answer:**

The slice `x[:]` creates a new outer list, so changing `y[0]` does not change `x`.

```bash
# Output:
# [10, 20, 30]
# [99, 20, 30]
```

## Common Mistakes With Python Lists

### Using the wrong index position

Python list indexes start at `0`, not `1`. New programmers often skip the first item or ask for a position that does not exist.

**Problem:** This code asks for index `3` in a three-item list. The last valid index is `2`, so Python raises an `IndexError`.

```python
letters = ["a", "b", "c"]
print(letters[3])
```

**Fix:** Use a valid index or compute the last item with `len()`.

```python
letters = ["a", "b", "c"]
print(letters[2])
print(letters[len(letters) - 1])
```

The corrected version works because both indexes point to a real item.

### Confusing append with extend

`append()` adds one item, even if that item is itself a list. `extend()` adds each element from the argument separately.

**Problem:** This code adds a whole list as one nested element, which is not what the programmer expected.

```python
values = [1, 2]
values.append([3, 4])
print(values)
```

**Fix:** Use `extend()` when you want the new items flattened into the list.

```python
values = [1, 2]
values.extend([3, 4])
print(values)
```

The corrected version works because `extend()` adds each element separately.

### Assigning the result of an in-place method

Methods like `sort()`, `reverse()`, and `append()` mutate the list and return `None`. Assigning that return value overwrites your variable with `None`.

**Problem:** This code replaces the list with `None`, so the original data is lost in the variable.

```python
numbers = [3, 1, 2]
numbers = numbers.sort()
print(numbers)
```

**Fix:** Call the method without assigning its return value, or use `sorted()` if you want a new list.

```python
numbers = [3, 1, 2]
numbers.sort()
print(numbers)

other_numbers = [3, 1, 2]
new_numbers = sorted(other_numbers)
print(new_numbers)
```

The corrected version works because the list stays in the variable, and `sorted()` returns a new list.

## Find the Bug

This code is meant to collect a student's quiz scores and print the average. One line has a subtle list bug. Find it.

```python
def add_score(score, scores=[]):
    scores.append(score)
    return scores

first_try = add_score(80)
second_try = add_score(90)
print(first_try)
print(second_try)
```

**Answer:**

The bug is the mutable default argument. The same list object is reused every time the function is called without an explicit `scores` argument, so both calls share state.

**Fixed code:**

```python
def add_score(score, scores=None):
    if scores is None:
        scores = []
    scores.append(score)
    return scores

first_try = add_score(80)
second_try = add_score(90)
print(first_try)
print(second_try)
```

The fixed version works because each call gets a fresh list unless one is passed in.

## When to Use a List, and When to Use Something Else

| Use a list when... | Use something else when... |
| --- | --- |
| You need ordered items that may change. | Use a tuple when the data should not change. |
| You need to add, remove, or reorder items. | Use a set when duplicates do not matter and membership checks are the main task. |
| You need indexed access by position. | Use a dictionary when you want to look up values by key instead of by position. |

Lists are the default choice for many sequence tasks. They are the right tool when order matters and the collection changes over time. If the values are fixed, a tuple can communicate that better. If you only care about unique membership, a set may be faster and clearer.

## Interview Questions About Python Lists

### What is the difference between a list and a tuple?

A list is mutable, so you can change its contents after creation. A tuple is immutable, so its values cannot be changed in place. Use a list for changing data and a tuple for fixed data.

### Why does `list.sort()` return `None`?

Because `sort()` changes the existing list in place. Python uses `None` as a signal that the method is doing a mutation instead of creating a new object. If you want a new sorted list, use `sorted()`.

### Why are slices useful?

Slices let you take part of a list without manually copying items in a loop. They are also a simple way to make a shallow copy of a list with `list[:]`.

## Try It Yourself

Create a list of three course names, then:

- print the second course,
- add a new course to the end,
- remove the first course,
- print the final list.

**Expected output:**

```python
History
['Math', 'Science', 'Art']
```

**Hint:** Remember that the second item is at index `1`, and `pop(0)` removes the first item.

**Solution:**

```python
courses = ["Math", "History", "Science"]
print(courses[1])

courses.append("Art")
courses.pop(0)

print(courses)

# Output:
# History
# ['History', 'Science', 'Art']
```

## Summary

- Python lists store ordered items and let you change them after creation.
- Indexes start at `0`, and slicing excludes the end position.
- `append()`, `extend()`, `insert()`, `pop()`, and `remove()` are core list operations.
- Many list methods change the list in place and return `None`.
- Simple assignment copies the reference, not the contents, so aliasing can surprise you.
- Use a list when order matters and the collection may need to change.

Next, compare Python lists with tuples to see when fixed data is the better choice.
