DEV Community

Cover image for Quark’s Outlines: Python Immutable Sequences
Mike Vincent
Mike Vincent

Posted on

Quark’s Outlines: Python Immutable Sequences

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Immutable Sequences

What is a Python immutable sequence?

You may want to store values that do not change. Python gives you immutable sequences for this need. These sequences hold a fixed list of items. You cannot add to them, change them, or remove from them once they are made.

A Python immutable sequence holds ordered values. You can still read the values. You can count the values using len(). You can use indexing like x[0] or slicing like x[1:3] to get parts of the sequence.

Python lets you store fixed ordered items with immutable sequences.

x = ("a", "b", "c")
print(x[0])        # prints: a
print(len(x))      # prints: 3
print(x[1:3])      # prints: ('b', 'c')
Enter fullscreen mode Exit fullscreen mode

What kinds of Python immutable sequences are built in?

Python gives you two main types of immutable sequences: strings and tuples.

A string holds text. It is made up of characters. A string is like "hello" or 'world'. Each character has a place, starting from index 0.

A tuple holds any Python objects. It is written with values inside parentheses and separated by commas. A tuple with one item needs a comma, like (5,).

Python lets you build strings and tuples as immutable sequences.

name = "Ada"
data = (1, 2, 3)
print(name[0])     # prints: A
print(data[-1])    # prints: 3
Enter fullscreen mode Exit fullscreen mode

You can loop over strings and tuples. You can join strings. You can combine tuples. But you cannot change any of their parts.

Why are Python immutable sequences useful?

Sometimes you want to keep values safe. You want to make sure no part of the data can be changed by accident. You might want to use the sequence as a key in a dictionary. Python only allows this when the sequence is immutable.

Immutable sequences also help you think clearly. Once made, they will not change. You know that x = (1, 2, 3) will always be those three values.

Python gives you safety and clarity with immutable sequences.

person = ("Ada", 36)
info = {person: "Scientist"}
print(info[("Ada", 36)])  # prints: Scientist
Enter fullscreen mode Exit fullscreen mode

A Historical Timeline of Python Immutable Sequences

Where do Python’s immutable sequence types come from?

Immutable sequences in Python follow a long history of fixed-size data structures. These types help keep values safe and make indexing and slicing simple. Python added these features slowly and kept their meaning stable.


People invented fixed sequences

1958 — String arrays in FORTRAN helped store text as fixed byte values in memory.

1960 — Index-based access in ALGOL used brackets to access elements by position.

People added immutable sequences to Python

1991 — Strings and tuples introduced Python 0.9.0 included str and tuple as fixed sequences with indexing and slicing.

2000 — Immutable used for safety Python 2.0 clarified which sequences could be dictionary keys.

People expanded and stabilized their use

2008 — Strings made Unicode by default Python 3.0 made text handling safer and more global.

2025 — No major changes Python core team kept immutable sequence rules clear and unchanged.


Problems & Solutions with Python Immutable Sequences

How do you use Python immutable sequences the right way?

Python immutable sequences help keep values fixed. They are good for text, settings, or data that must not be changed. These problems show how Python uses them in everyday needs.


Problem: How do you save a name that should not change in Python?

You are writing a user profile. The user’s birth year must stay the same. You want to store this together with their name. You do not want the values to be changed by mistake later in the code.

Problem: You need to store a pair of values that should not change.

Solution: Use a tuple to keep the values fixed.

Python lets you group constant data using a tuple.

user = ("Ada", 1985)
print(user[0])     # prints: Ada
print(user[1])     # prints: 1985
Enter fullscreen mode Exit fullscreen mode

Problem: How do you write a greeting with fixed text in Python?

You want to show a message on screen that says “Hello, world!” This message never changes. You want to print it each time the program runs.

Problem: You need to store a short fixed message.

Solution: Use a string to hold the text.

Python lets you store fixed text using a string.

greeting = "Hello, world!"
print(greeting)    # prints: Hello, world!
Enter fullscreen mode Exit fullscreen mode

Problem: How do you get part of a value in Python?

You want to read just part of a longer value. For example, the first three letters of a name. You want the rest to stay unchanged.

Problem: You need to access a slice of a value without changing it.

Solution: Use slicing on a string or a tuple.

Python lets you read parts of immutable sequences using slices.

name = "Michael"
print(name[0:3])   # prints: Mic

scores = (90, 85, 88, 92)
print(scores[1:3]) # prints: (85, 88)
Enter fullscreen mode Exit fullscreen mode

Problem: How do you protect values used as dictionary keys in Python?

You are building a dictionary to store information about people. You want to use their name and year of birth as the key. The key must not change.

Problem: You need to use a sequence as a dictionary key.

Solution: Use an immutable tuple as the key.

Python lets you use tuples as keys in dictionaries.

key = ("Ada", 1985)
record = {key: "Engineer"}
print(record[("Ada", 1985)])  # prints: Engineer
Enter fullscreen mode Exit fullscreen mode

Problem: How do you prevent unwanted changes to data in Python?

You give a list of values to another part of the program. You do not want that code to change your data. But if you use a list, someone might change it.

Problem: You need to pass values without risk of mutation.

Solution: Use a tuple instead of a list.

Python lets you protect data from changes using tuples.

def show(data):
    print(data)

items = (1, 2, 3)
show(items)        # prints: (1, 2, 3)
Enter fullscreen mode Exit fullscreen mode

If the data were a list, it could be changed. As a tuple, it stays the same.


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 software engineer and app developer from Los Angeles, California. More about Mike Vincent

Top comments (0)