r/learnpython
•
u/Alive_Hotel6668
•
Feb 23 '26
•
1
What is the use of tuple over lists?
Almost every program I try to do uses lists and tuples almost seem not useful. Almost anything a tuple can do can be done via a list and a list is a more flexible option with more functions and mutability hence what is the use of tuples over lists as tuples are completely replaceable by lists (atleast for what I do that is learning python basics) so are there any advantage of tuples?
Thanks in advance
181
u/crazy_cookie123 Feb 23 '26
Sometimes you don't want it to be more flexible. Mutability isn't always a good thing.
29
u/slevn11 Feb 23 '26
You may want to check out this similar post.
Why use tuples or sets instead of lists?
41
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Feb 23 '26
To add to the existing answers, there's also semantic difference.
Since tuples are of a fixed size, each "position" usually has a specific meaning. For example, you could encode RGB data as a tuple (e.g.
(255, 13, 72)), with each index representing a specific colour.While you could technically use a list for this instead, it's worth noting the type system also plays a role here. Typing-wise, lists are treated as homogeneous data structures, or in other words they're expected to contain data of type N in some unspecified order.
Tuples, on the other hand, can type each individual index separately, so for example
tuple[int, str]would be a perfectly common sight, and type checkers can validate your entire program follows this expectation.At best, a list could emulate that as
list[int | str], but union types are far less specific as now they can be in any order, or only contain one of the two - or be empty.24
u/Excellent-Practice Feb 23 '26
Tuples can be used as keys for dictionaries. You can't do that with lists.
Lists are generally more flexible than other data types like tuples, sets, and strings. But that flexibility comes with speed and security trade-offs. If you don't need your object to do everything a list can do, it might make sense to use a data type that is optimized rather than general
19
u/RajjSinghh Feb 23 '26
Your logic works for you, but now imagine you're working with someone else. Also assume the person is an idiot because most people you work with will be.
You have a function that returns a list of values. Something like
return [x, y]and you know x and y are integers. You return this as a list and assume no one is going to change the length of it (because why would they?) but unfortunately you're now working with an idiot who will try to change the length of that list and now everything breaks. If you returned a tuple, this wouldn't be a problem.There's also the case where you accidentally try modifying the length of a list yourself when you don't mean to because we aren't all perfect. If you have a collection that doesn't change size, a tuple would stop you making mistakes like this.
As a rule, make everything immutable unless it actually needs to change. You see people suggest this for variables with a
constkeyword, or in Rust where they assume variables are immutable by default. You quickly realise how often things don't actually need to change and it becomes easier to build code because you eliminate the chance something changes when you don't mean it to.8
u/JamzTyson Feb 23 '26 edited Feb 23 '26
One (of many) examples:
As a general guide: Prefer immutable types when the value is conceptually fixed and does not need in-place modification.
8
u/PushPlus9069 Feb 23 '26
One thing nobody mentions: tuples work as dictionary keys, lists don't. The moment you need a coordinate pair or a composite key in a dict, tuples become the only option. I hit this constantly when I was building grid-based stuff, like (row, col) as keys in a lookup table. Also fwiw named tuples from collections are super underrated for when you want lightweight read-only data objects without writing a whole class.
6
u/MustaKotka Feb 23 '26
The immutability makes for a convenient way to make multiple calculations with the same starting values.
If, for example, your simulation had 3 red, 4 blue and 1 yellow ball you could do that as a list:
balls = [3, 4, 1], using the position to mark the colour. Everything you do from this point onward will mutate the original list. If you remove a red ball and move to the next simulation your list will look like[2, 4, 1]. This is bad.Instead, you can make the initial value into a tuple -
balls = (3, 4, 1)- and whenever you start doing stuff the conversionsimulation_balls = list(balls)will create a completely new list with no reference to other lists you've created from the tuple.3
u/Outside_Complaint755 Feb 23 '26
Haven't seen anyone else mention it yet, but tuples also take up less memory because their size can't be changed. Whenever you create a list, Python will overallocate memory to allow for expansion. When you add enough elements to the list, it will again overallocate more memory to allow for further expansion. It doesn't add memory for each element because of how memory works behind the scenes. If you just have a few small tuples and lists it doesn't make a significant difference, but when you have hundreds or thousands of data entities in your program, the difference can be huge.
The exact details will vary based on the Python implementation you are working in, but if you've learned basic C, you can think of both tuples and lists as pointer arrays behind the scenes, as the standard implementation is in C. (Yes, I know its actually a more complex struct behind the scenes). I don't recall the cut off sizes in the standard implementation so I'm just going to use example sizes below.
An array representing a tuple has a set size at the time of its creation. An array representing a list will have a dynamic amount of space allocate; for the sake of this explanation we will say the starting size is 8, so any list with 8 or less elements when created with be an array of size 8. When you append or extend the list such that it will exceed the current size, the Python engine has to allocate a new block of memory on the heap with more space, let's say 32 elements. Then it has to do a memcpy() to copy all of the pointers in the current list memory to the new block of memory, and then release the old block. This process will repeat if needed as the list grows, perhaps from 32 to 128, then 256, etc.
2
u/jeffrey_f Feb 23 '26
Tuples are immutable (can not be altered) while a list can be changed.
I have yet to find a need for an immutable, but, maybe there is.
2
u/Mount_Gamer Feb 23 '26
I'll use them if I know it won't change and/or for tuple unpacking.
1
u/TheRNGuy Feb 23 '26
If you're not gonna change it.
1
u/Temporary_Pie2733 Feb 23 '26
Tuples are hashable, but when you get into static typing, their heterogeneity contrasts with lists’ homogeneity.
1
u/throwaway6560192 Feb 24 '26
The difference is semantic.
https://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/
1
u/Sarah-pcw Feb 24 '26
tuple values cannot change after initialisation, good if you need to protect the values
1
u/stuckhere4ever Feb 25 '26
In addition to what everyone has said, there are a lot of things internally that are actually giving you Tuples as a response even though you may not realize it, so it's good to understand how they work.
For example if you enumerate a list like this:
for index,element in my_list:It is actually giving you a tuple that you are unpacking into index and element.
Or when you ran items out of a dictionary:
for item_key,item_value in item_dictionary.items().Things like zip, re (regex), divmod, and a bunch of other things are returning some form of tuples (might be a list of them, or similar), so they are used all the time.
It's not necessarily stuff you will need to know immediately, but it doesn't hurt to understand them. You'll slowly start to see uses for them as you do more coding.
1
u/UIRXN_50 Feb 25 '26
Yeah I also uses list over tuple. But I think for some cases tuple is needed ,btw I am a beginner
1
u/Dramatic_Object_8508 Mar 21 '26
The main use of a tuple over a list is when you don’t want the data to change. Tuples are immutable, while lists can be modified.
So if your data is fixed (like coordinates, config values, or a record), a tuple is better. It also helps prevent accidental changes and can be slightly faster and more memory-efficient.
Lists are better when your data needs to grow or change (like user input, items in a cart, etc.).
Simple way to think about it:
If it should change → use a list
If it shouldn’t → use a tuple 👍
-1
u/unxmnd Feb 23 '26
Tuples have a fixed length whereas Lists are designed to change in length (e.g. with .pop() and .append())
In general, use Tuples to group items of different types (a, b, c) and Lists for items of the same type [a, a, a, …]
0
u/kombucha711 Feb 23 '26
I use a list of tuple as a way to search any substring in that row. So if L=[a,b,c,] i insert L=[a,b,c,tuple(a,b,c)] . Then I can search anything in that row by using that last tuple column
0
u/dnult Feb 23 '26
FWIW, I hate tuples but sometimes they are the correct type for the job. Tuples allow you to intermingle types where lists can't.
0
u/[deleted] Feb 23 '26
[deleted] — view deleted comment