Overview, Historical Timeline, Problems & Solutions
An Overview of Python Mutable Sequences
What is a Python mutable sequence?
When you write a list in Python, you create a mutable sequence. A mutable sequence is a group of items that you can change. You can add items, remove items, or change an item’s value after the list is created.
Python mutable sequences are ordered. This means each item has a number called an index. You can use the index to get or change the item. The first item has index 0
. The second item has index 1
, and so on.
Python lets you build lists as mutable sequences.
letters = ["a", "b", "c"]
letters[0] = "z"
print(letters)
# ['z', 'b', 'c']
In this example, the item at index 0
changes from "a"
to "z"
. The list is mutable, so the change works.
What kinds of Python sequences are mutable?
Python has one built-in mutable sequence: the list. A list uses square brackets to hold items. Each item can be any Python object.
You can also create other mutable sequences using modules like array
, but lists are the most common.
Python gives you lists to store and change many values.
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
# [1, 2, 3, 4]
You can use append()
to add items. You can also use del
to remove items, or insert()
to place items at a certain index.
What operations can you use on Python mutable sequences?
You can change a mutable sequence in many ways. You can assign a new value to an item. You can delete items. You can add new items. You can even use slice notation to change several items at once.
Python lets you change multiple items with slicing.
colors = ["red", "green", "blue"]
colors[1:3] = ["yellow", "purple"]
print(colors)
# ['red', 'yellow', 'purple']
This replaces the items at index 1
and 2
with two new colors.
A Historical Timeline of Python Mutable Sequences
Where do Python’s mutable sequences come from?
Mutable sequences in Python come from early ideas about arrays and lists. Python gave these ideas a simple, readable form. The list type became a core part of the language and has changed little over time.
People invented ways to store groups of values.
1956 — FORTRAN arrays let you store sets of values in fixed-size blocks.
1972 — C language arrays and pointers let programs work with values in memory by index.
1980 — ABC language tables and sequences added readability with high-level data structures.
People designed Python’s first mutable sequence.
1991 — Python lists were added in version 0.9.0 as a flexible way to store many items.
2000 — List comprehensions in Python 2.0 made it easier to build and fill lists.
People added new ways to change sequences.
2001 — Extended slicing gave more control over list changes in Python 2.2.
2007 — List copy shortcut [:]
became a common idiom for making a shallow copy.
2010 — collections.deque
gave Python a list-like structure with fast appends at both ends.
People kept Python lists stable.
2025 — Python list behavior remains stable and is used in nearly all real-world Python programs.
Problems & Solutions with Python Mutable Sequences
How do you use Python mutable sequences the right way?
Python mutable sequences help you manage data that needs to change. Lists are common in programs that grow or shrink as they run. Each problem below shows a need you might face and how a mutable sequence can solve it.
Problem: How do you store a changing list of names in Python?
You are keeping track of who joins a group. As new people arrive, you need to add their names. The group may grow at any time. You need a data structure that lets you add names when needed.
Problem: You need to grow a list of names as people join.
Solution: Use a Python list and the append()
method.
Python lets you add items to a mutable sequence.
group = []
group.append("Ada")
group.append("Linus")
print(group)
# ['Ada', 'Linus']
The list starts empty. Each call to append()
adds a name to the end.
Problem: How do you update part of a sequence in Python?
You have a list of tasks. One of the tasks needs to change because the plan has changed. You do not want to make a new list. You just want to update one item.
Problem: You need to change a value in place.
Solution: Use index assignment to replace one item.
Python lets you change items in a mutable sequence.
tasks = ["write", "test", "deploy"]
tasks[1] = "review"
print(tasks)
# ['write', 'review', 'deploy']
This updates the second item (index 1) from "test"
to "review"
.
Problem: How do you delete an item from a list in Python?
You are working with a list of files. One of the files was removed. You want to update your list to reflect that. You do not need to keep the old name.
Problem: You need to remove an item from the list.
Solution: Use del
with an index to delete it.
Python lets you delete items from a mutable sequence.
files = ["a.txt", "b.txt", "c.txt"]
del files[1]
print(files)
# ['a.txt', 'c.txt']
This removes the item at index 1
, which was "b.txt"
.
Problem: How do you change multiple items in a list at once in Python?
You have a list of grades. The teacher wants to replace the second and third grades with new ones. Instead of changing them one by one, you want to do it in one step.
Problem: You need to change part of a list with new values.
Solution: Use slice assignment.
Python lets you replace slices in a mutable sequence.
grades = [90, 80, 70, 60]
grades[1:3] = [85, 75]
print(grades)
# [90, 85, 75, 60]
This replaces the items at index 1
and 2
with two new numbers.
Problem: How do you sort and reverse a list in place in Python?
You have a list of scores from a game. You want to show the list in order from highest to lowest. You do not want to make a new list. You want to change the original one.
Problem: You need to sort and reverse the list.
Solution: Use sort()
and reverse()
on the list itself.
Python lets you sort and reverse mutable sequences in place.
scores = [42, 17, 99, 8]
scores.sort()
scores.reverse()
print(scores)
# [99, 42, 17, 8]
This changes the order of the original list to show the highest scores first.
Like, Comment, Share, and Subscribe
Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!
Mike Vincent is an American consumer software application developer from Los Angeles, California. More about Mike Vincent
Top comments (0)