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.

Example: Create an empty tuple

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

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

Example: Convert string into tuple

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

Example: Convert list into tuple

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

Example: Create tuple of successive integers

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

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

Example: Convert dictionary keys into tuple

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

Example: Convert set into tuple

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