In Python, tuple() is a built-in function also known as tuple constructor, used to create a tuple from an iterable such as a list, string or range. A tuple is an ordered and immutable sequence type. For Example:
tup = [1,2,3]
print(tuple(tup))
Output
(1, 2, 3)
Syntax
tuple(iterable)
Parameter: iterable (optional) - Any iterable object such as list, string, set, range or iterator.
- If provided, a new tuple is created from its elements.
- If not provided, an empty tuple is returned.
Create tuples using tuple()
This code shows how the tuple() function behaves with different inputs. It creates an empty tuple when no value is passed and converts other iterables like lists and strings into tuples.
# when parameter is not passed
a = tuple()
print("Empty Tuple:", a)
# when an iterable(e.g., list) is passed
b = [ 1, 2, 3, 4 ]
c = tuple(b)
print("List to Tuple:", c)
# when an iterable(e.g., string) is passed
s = "geeksforgeeks";
d = tuple(s)
print("String to Tuple:", d)
Output
Empty Tuple: ()
List to Tuple: (1, 2, 3, 4)
String to Tuple: ('g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's')
Errors when using Tuple
This example shows what happens when you pass a non-iterable value to tuple(). Since integers are not iterable, Python raises a TypeError.
tup = tuple(1)
print(tup)
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 1, in <module>
TypeError: 'int' object is not iterable
Accessing Values in Tuples
This example creates a tuple and retrieves a slice of elements using index positions. Tuple slicing works the same way as lists.
tup = (1, 2, 3, 4, 5)
print(tup[1:4])
Output
(2, 3, 4)
Explanation:
- tup[1:4] extracts elements starting from index 1 up to 3 (4 is excluded).
- So it returns the values 2, 3, and 4 as a new tuple.
Deleting a Tuple
This example shows what happens when a tuple is deleted using del. After deletion, trying to access the tuple again causes an error because it no longer exists in memory.
tup = (1, 2, 3, 4, 5)
del tup
print(tup)
Output
ERROR!
Traceback (most recent call last):
File "<main.py>", line 3, in <module>
NameError: name 't' is not defined
Explanation:
- del tup removes the tuple entirely from memory.
- After deletion, tup is undefined, so accessing it raises NameError.
Creating a Tuple From dict
This example shows how to convert a dictionary into a tuple. The items() method returns key–value pairs and tuple() converts them into a tuple of pairs.
d = {'apple': 1, 'banana': 2, 'cherry': 3}
tup = tuple(d.items())
print(tup)
Output
(('apple', 1), ('banana', 2), ('cherry', 3))
Explanation:
- d.items() returns key–value pairs like ('apple', 1)
- tuple(d.items()) converts these pairs into a tuple of tuples.
Tuple() Built-In Functions
1. len(): len() function returns the total number of items present in a tuple. It helps you quickly check how many elements the tuple contains.
tup = tuple((1, 2, 3))
print(len(tup))
Output
3
2. max(): max() function returns the largest value from the tuple. This works only when all elements are comparable (numbers, characters, etc.).
tup = tuple((1, 2, 3))
print(max(tup))
Output
3
3. min(): min() function returns the smallest value from the tuple. Like max(), this works when the tuple contains comparable data.
tup = tuple((1, 2, 3))
print(min(tup))
Output
1
4. sum(): sum() function adds all the numeric elements in the tuple and returns the total. It works only for tuples containing numbers.
tup = tuple((1, 2, 3))
print(sum(tup))
Output
6
5. sorted(): sorted() function sorts the elements of the tuple in ascending order and returns a new list. If you want the result as a tuple, you can convert the list back into a tuple.
tup = tuple((3, 2, 1))
res = tuple(sorted(tup))
print(res)
Output
(1, 2, 3)
Advantages Of Tuple Over List
Python supports both tuples and lists as data structures that may be used to hold a collection of data. Nevertheless, there are certain benefits to utilising tuples rather than lists.
- Tuples cannot be changed.
- Tuples are faster than lists.
- Tuples can be used as dictionary keys.