Python tuple() Function

Creates a tuple from an iterable

Usage

The tuple() function creates a tuple from an iterable.

The iterable may be a sequence (such as a string, list or range) or a collection (such as a dictionary, set or frozen set)

Syntax

tuple(iterable)

Python tuple() function parameters
ParameterConditionDescription
iterableRequiredA sequence or a collection

Examples

tuple() with no arguments creates an empty tuple.

T = tuple()
print(T)
# Prints ()

You can convert any sequence (such as a string, list or range) into a tuple using a tuple() method.

# string into tuple
T = tuple('abc')
print(T)
# Prints ('a', 'b', 'c')

# list into tuple
T = tuple([1, 2, 3])
print(T)
# Prints (1, 2, 3)

# sequence into tuple
T = tuple(range(0, 4))
print(T)
# Prints (0, 1, 2, 3)

You can even convert any collection (such as a dictionary, set or frozen set) into a tuple.

# dictionary keys into tuple
T = tuple({'name': 'Bob', 'age': 25})
print(T)
# Prints ('age', 'name')

# set into tuple
L = tuple({1, 2, 3})
print(L)
# Prints (1, 2, 3)