Python tuple() Function Explained
The built-in tuple() function creates a tuple in Python.
You can use it in two main ways:
tuple()creates an empty tupletuple(iterable)converts an iterable into a tuple
This is useful when you want a fixed, immutable sequence of values. Beginners often use tuple() to convert a list, string, range, or other iterable into a tuple.
Quick example
numbers = [1, 2, 3]
result = tuple(numbers)
print(result)
print(type(result))
Output:
(1, 2, 3)
<class 'tuple'>
Use tuple(iterable) to convert an iterable like a list, string, or range into a tuple.
What tuple() does
tuple() is a built-in Python function.
It:
- creates a tuple object
- can make an empty tuple with no arguments
- can convert another iterable into a tuple
If you are new to tuples, see what is a tuple in Python or the full guide to Python tuples explained.
Basic syntax
There are two common forms:
tuple()
tuple(iterable)
Important points:
- The argument must be an iterable
- Common iterables include:
- lists
- strings
- ranges
- sets
- dictionaries
An iterable is a value Python can go through one item at a time.
Create an empty tuple
Calling tuple() with no argument returns an empty tuple.
empty = tuple()
print(empty)
print(type(empty))
Output:
()
<class 'tuple'>
This is equivalent to:
empty = ()
Convert common iterables to a tuple
List to tuple
A list keeps its item order when converted to a tuple.
numbers = [10, 20, 30]
result = tuple(numbers)
print(result)
Output:
(10, 20, 30)
If you need a changeable sequence instead, see the Python list() function explained.
String to tuple
A string is iterable, so each character becomes a separate item.
text = "cat"
result = tuple(text)
print(result)
Output:
('c', 'a', 't')
This often surprises beginners. tuple("cat") does not create ('cat',).
Range to tuple
range() produces values one at a time, and tuple() collects them all.
result = tuple(range(5))
print(result)
Output:
(0, 1, 2, 3, 4)
Set to tuple
A set can also be converted to a tuple.
values = {1, 2, 3}
result = tuple(values)
print(result)
Output:
(1, 2, 3)
The order may not be predictable, because sets do not store items in a fixed order. If you want to understand sets better, see the Python set() function explained.
Dictionary to tuple
When you pass a dictionary to tuple(), Python uses the dictionary's keys.
person = {"name": "Ana", "age": 25}
result = tuple(person)
print(result)
Possible output:
('name', 'age')
What happens with dictionaries
This is a common beginner surprise.
tuple(my_dict) returns a tuple of dictionary keys, not key-value pairs.
person = {"name": "Ana", "age": 25}
print(tuple(person))
print(tuple(person.items()))
Output:
('name', 'age')
(('name', 'Ana'), ('age', 25))
Use:
tuple(my_dict)for keystuple(my_dict.items())for key-value pairs
This happens because iterating over a dictionary gives its keys by default. You can learn more in the Python dict() function explained.
When to use tuple()
tuple() is a good choice when:
- you need an immutable sequence
- you want to convert a list into a tuple
- you want to store a fixed group of values
- you have data that should not be changed later
Example:
coordinates = tuple([4, 7])
print(coordinates)
Output:
(4, 7)
Tuples are often used for values like:
- coordinates
- RGB colors
- dates
- settings that should stay fixed
When not to use tuple()
Do not use tuple() when:
- you need to add, remove, or update items later
- a list would be easier to work with
- you are passing a non-iterable value
For example, this does not work:
tuple(5)
An integer is not iterable, so Python raises an error.
If you need a flexible sequence, use a list instead of a tuple.
Common errors and confusion
Here are some common problems beginners run into.
Passing a non-iterable value
This causes an error:
tuple(5)
Error:
TypeError: 'int' object is not iterable
If you see this, read TypeError: int object is not iterable.
Expecting one whole string item
This code:
tuple("hello")
returns:
('h', 'e', 'l', 'l', 'o')
It does not return:
('hello',)
If you want a one-item tuple, write:
single = ("hello",)
print(single)
Expecting dictionary values too
This code:
data = {"a": 1, "b": 2}
print(tuple(data))
returns only the keys:
('a', 'b')
To get key-value pairs, use:
print(tuple(data.items()))
Thinking parentheses always convert to a tuple
Parentheses can create tuples in many cases, but they do not behave the same as tuple() conversion.
For example:
value = (1, 2, 3)
print(value)
This creates a tuple directly.
But tuple() is specifically used to create an empty tuple or convert an iterable into a tuple.
FAQ
What does tuple() do in Python?
It creates a tuple. With an iterable argument, it converts that iterable into a tuple.
What does tuple() return with no argument?
It returns an empty tuple: ().
Can tuple() convert a string?
Yes. Each character becomes a separate item in the tuple.
Why does tuple(my_dict) only return keys?
Iterating over a dictionary gives its keys by default.
Can I use tuple() on an integer?
No. An integer is not iterable, so Python raises a TypeError.