All jobs / Backend developer / Backend Languages
Backend developer interview question · 0–1 years
When would you choose a tuple over a list?
The strongest answer
When the collection is fixed and its position means something — a coordinate, a record, a dict key — since tuples are immutable and therefore hashable
Why it works
Right. Immutability is the point, and hashability is what it buys you.
What a good answer covers
- Tuple = fixed, positional, hashable.
- List = a sequence you intend to change.
- Only a tuple can key a dict or join a set.
- Choose on meaning, not on length.
Answers that fall short
- Close, but not the strongest: Whenever I want the code to run faster; tuples are always faster
There is a small difference, but speed is not the reason to choose one. - Weak: Never; a list does everything a tuple does
A list cannot be a dict key or a set member; that alone decides some cases. - Red flag: When I have fewer than ten items
Size has nothing to do with it.
Who gets asked this
- Backend developer
- Full-stack developer
- Automation developer
- DevOps / SRE / cloud
- Data analyst / BI
- Data engineer
- Data science / ML / AI
- AI engineer
More Backend Languages questions
- What is the most common cause of a NullReferenceException in a new codebase?
- What is the difference between == and is?
- When do you use an array rather than a List<T>?
- You want a value from a dict, or a default if the key is missing, without raising. What do you use?
- You remove items from a List<T> inside a foreach over it and get an exception. Why?
- if items: — what exactly is being tested?
- When is var a good idea?
- Building a long string in a loop with s += piece is slow on big inputs. Why?