DEV Community

Feddy Mwanjumwa
Feddy Mwanjumwa

Posted on

Python Tuples: What I Learned About Fixed Data

After learning lists, I came across tuples.

At first, I honestly wondered why Python needed tuples when lists already existed.

They looked almost the same.

For example, a list looks like this:

products = ["Bread", "Milk", "Sugar"]
And a tuple looks like this:

products = ("Bread", "Milk", "Sugar")

The big difference I eventually understood is that lists can be changed, while tuples cannot.

That one idea made tuples much easier to understand.

Creating a tuple

A tuple is created using parentheses:

fruits = ("Apple", "Mango", "Banana")

I can also create a tuple with numbers:

numbers = (10, 20, 30, 40)

Or different types of values:

person = ("Feddy", 20, True)

Just like lists, tuples can store multiple values.

Accessing items

Tuples use indexing just like lists.

Python starts from 0.

For example:

fruits = ("Apple", "Mango", "Banana")

print(fruits[0])

The result is:

Apple

I can also use negative indexing:

print(fruits[-1])

This gives:

Banana

So the way I access tuple items is almost exactly the same as with lists.

The main difference: tuples cannot be changed

This is where tuples become different.

Suppose I have:

coordinates = (10, 20)

I can't do this:

coordinates[0] = 50

Python will give me an error because tuples are immutable.

That simply means once the tuple is created, I cannot change its individual values.

This was the main thing that helped me understand when to use a tuple.

Why would I want data that cannot change?

At first, I thought being unable to change the data was a disadvantage.

Then I started thinking about information that should stay fixed.

For example, coordinates:

location = (-1.286389, 36.817223)

Or the days of the week:

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

I don't want my program accidentally changing these values.

That's where tuples make sense.

Tuple methods

Tuples don't have as many methods as lists because they cannot be modified.

The two main ones I use are count() and index().

For example:

numbers = (10, 20, 20, 30, 20)

I can count how many times 20 appears:

print(numbers.count(20))

The result is:

3

I can also find the position of an item:

print(numbers.index(30))

The result is:

3

That's basically it for the most common tuple-specific methods.

Checking if an item exists

Just like with lists, I can use in.

For example:

days = ("Monday", "Tuesday", "Wednesday")

print("Monday" in days)

The result is:

True

I can also use it in a condition:

if "Monday" in days:

print("It's a weekday")

Finding the length

I can use len() with tuples too.

coordinates = (10, 20)

print(len(coordinates))

The result is:

2

So functions such as len() work with both lists and tuples.

Slicing tuples

Tuples can also be sliced.

For example:

numbers = (10, 20, 30, 40, 50)

print(numbers[1:4])

The result is:

(20, 30, 40)

Just like with lists, the starting index is included and the ending index is not.

Looping through a tuple

I can use a for loop with a tuple.

For example:

days = ("Monday", "Tuesday", "Wednesday")

for day in days:

print(day)

Python goes through the tuple one item at a time.

This is useful when I have a fixed collection of values that I still need to process.

Tuple unpacking

This was one of the more interesting things I learnt about tuples.

I can assign the values of a tuple to separate variables.

For example:

student = ("Feddy", 20, "Data Science")

I can write:

name, age, course = student

Now:

name is "Feddy"

age is 20

course is "Data Science"

I can then do:

print(name)

print(age)

print(course)

This is called tuple unpacking.

Once I understood it, I started noticing how useful it could be when working with multiple values at once.

Returning multiple values from a function

Tuple unpacking also makes sense when working with functions.

For example:

def get_student():

return "Feddy", 20, "Data Science"

I can then do:

name, age, course = get_student()

Python returns the three values together, and I can unpack them into separate variables.

I found this interesting because I don't have to create three separate variables inside the function just to return them.

A tuple with one item

There is one small thing that caught me out.

This:

numbers = (10)

is not actually a tuple.

Python treats it as a normal number.

For a single-item tuple, I need a comma:

numbers = (10,)

That comma is important.

This is one of those small Python details I only really remembered after using it.

Tuples can contain mutable objects

The tuple itself cannot be changed, but it can contain something that can be changed.

For example:

student = ("Feddy", ["Python", "SQL"])

I can't replace "Feddy" with another name.

But the list inside the tuple can be changed:

student[1].append("Power BI")

Now the tuple contains:

("Feddy", ["Python", "SQL", "Power BI"])

This was a good reminder that immutable doesn't always mean absolutely everything inside can never change.

Lists vs tuples

This became much clearer to me when I stopped thinking about syntax and started thinking about the data.

A list:

products = ["Bread", "Milk", "Sugar"]

A tuple:

shop_location = ("Nairobi", "Kenya")

The products might change.

The location probably shouldn't.

So I started thinking about it like this:

List → data I may need to modify.

Tuple → data I want to keep fixed.

A practical example

Imagine I'm building a small application for a shop.

I could store the shop location as:

shop_location = ("Nairobi", "Kenya")

I can access the city:

print(shop_location[0])

And the country:

print(shop_location[1])

But I don't want another part of the program accidentally changing the location.

For products, I would probably use a list instead:

products = ["Bread", "Milk", "Sugar"]

because products can be added or removed.

This helped me understand that choosing between a list and a tuple isn't about which one is better.

It's about what I want the data to do.

What I understood

The biggest thing I learnt about tuples is that they are useful for data that shouldn't be changed.

I can still:

  • Access items
  • Slice them
  • Loop through them
  • Check for values
  • Count values
  • Find positions
  • Unpack values

But I can't simply change one of the items after creating the tuple.

Final thoughts

At first, tuples felt unnecessary because they looked so similar to lists.

But once I understood they are immutable , their purpose became much clearer.

I don't reach for a tuple because it has more features than a list.

Top comments (0)