🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

tuple()

AI & DATA SCIENCE // tuple

tuple() builds a new immutable, ordered sequence, either empty or from the elements of any iterable.

Syntax

tuple()
tuple(iterable)

Deep Dive Course

A tuple behaves like a list — ordered, indexable, allows duplicates — but once created it can't be modified: no append, no item assignment. That immutability is exactly why tuples, unlike lists, can be used as dictionary keys or set members, as long as every element inside them is itself hashable. tuple(iterable) eagerly consumes the iterable and copies its elements into the new, fixed sequence.

1Understanding tuple()

A tuple behaves like a list — ordered, indexable, allows duplicates — but once created it can't be modified: no append, no item assignment. That immutability is exactly why tuples, unlike lists, can be used as dictionary keys or set members, as long as every element inside them is itself hashable. tuple(iterable) eagerly consumes the iterable and copies its elements into the new, fixed sequence.

💡

A single-element tuple needs a trailing comma — parentheses around one value alone is just that value, while adding a comma makes it a one-item tuple.

editor.html
coordinates = tuple([3, 4])
print(coordinates)
single = (5,)
print(single, type(single))
localhost:3000

2Practical Example

Here is a real-world application of tuple() showing how it is used in production Python code.

editor.html
distances = {}
distances[(0, 0)] = 0
distances[(1, 1)] = 1.41
print(distances[(1, 1)])
localhost:3000

3Best Practices

Follow these guidelines when working with tuple():

1. Use a tuple instead of a list for fixed collections that shouldn't change, like coordinates or RGB values, to signal that intent

2. Use tuples as dictionary keys when you need a composite key made of multiple values

3. Prefer named tuples (collections.namedtuple) over plain tuples when the fields need readable names

⚠️

Tip: A single-element tuple needs a trailing comma — parentheses around one value alone is just that value, while adding a comma makes it a one-item tuple.

editor.html
coordinates = tuple([3, 4])
print(coordinates)
single = (5,)
print(single, type(single))
localhost:3000

Examples

Example 01Basic Usage
coordinates = tuple([3, 4])
print(coordinates)
single = (5,)
print(single, type(single))
Example 02Advanced Example
distances = {}
distances[(0, 0)] = 0
distances[(1, 1)] = 1.41
print(distances[(1, 1)])

Best Practices

  • Use a tuple instead of a list for fixed collections that shouldn't change, like coordinates or RGB values, to signal that intent
  • Use tuples as dictionary keys when you need a composite key made of multiple values
  • Prefer named tuples (collections.namedtuple) over plain tuples when the fields need readable names

Interview Question

Why are tuples allowed as dictionary keys while lists are not?

Hint: It's the same reason lists can't go inside a set.

Dictionary keys must be hashable, and hashability requires the object's value never to change after creation — otherwise its hash could go stale and the dict's internal bucket lookup would break. Tuples are immutable, so, as long as their contents are also hashable, they satisfy that requirement, while lists are mutable and therefore explicitly unhashable.

Exercises

MediumPractice using tuple() in a real scenario.
View Solution
coordinates = tuple([3, 4])
print(coordinates)
single = (5,)
print(single, type(single))

Frequently Asked Questions

Why are tuples allowed as dictionary keys while lists are not?

Dictionary keys must be hashable, and hashability requires the object's value never to change after creation — otherwise its hash could go stale and the dict's internal bucket lookup would break. Tuples are immutable, so, as long as their contents are also hashable, they satisfy that requirement, while lists are mutable and therefore explicitly unhashable.

Related Functions

list()namedtupledictionaries