Skip to main content

Python tuple() Function

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)

danger

You can not change or remove items in a tuple.

Syntax

tuple(iterable)

tuple() Parameters

Python tuple() function parameters:

ParameterConditionDescription
iterableOptionalA sequence or a collection

tuple() Return Value

Python tuple() function returns a tuple:

  • If no parameters are passed, it returns an empty tuple
  • If iterable is passed as a parameter, it creates a tuple consisting of iterable's items.

Examples

Example 1: Creating an Empty Tuple

Let's create an empty Tuple using tuple() without argument.

empty_tuple = tuple()
print(empty_tuple) # Output: ()

output

()

Example 2: Creating a Tuple from a String

Let's create a Tuple from a String using tuple():

string = "ABCDE"
my_tuple = tuple(string)
print(my_tuple) # Output: ('A', 'B', 'C', 'D', 'E')

output

('A', 'B', 'C', 'D', 'E')

Example 3: Creating a Tuple from a Tuple

Let's create a Tuple from a Tuple using tuple():

tuple1 = ('A', 'B', 'C', 'D', 'E')
my_tuple = tuple(tuple1)
print(my_tuple) # Output: ('A', 'B', 'C', 'D', 'E')

output

('A', 'B', 'C', 'D', 'E')

Example 4: Creating a Tuple from a Set

Let's create a Tuple from a Set using tuple():

set1 = {1,  2,  3,  4,  5}
my_tuple = tuple(set1)
print(my_tuple) # Output: (1, 2, 3, 4, 5)

output

(1, 2, 3, 4, 5)
note

The order of the elements in the tuple may vary because sets are unordered collections!

Example 5: Creating a Tuple from a Dictionary

Let's create a Tuple from a Dictionary using tuple():

dict1 = {'one':  1, 'two':  2, 'three':  3}
list1 = list(dict1)
print(list1) # Output: ('one', 'two', 'three')

output

['one', 'two', 'three']

Example 6: Creating a Tuple from a List

Let's create a Tuple from a List using tuple():

list1 = ['a', 'b', 'c', 'd', 'e']
new_tuple = tuple(list1)
print(new_tuple) # Output: ('a', 'b', 'c', 'd', 'e')

output

('a', 'b', 'c', 'd', 'e')

Example 7: Creating a Tuple from a Range

Let's create a Tuple from a Range using tuple():

my_tuple = tuple(range(0, 5))
print(my_tuple) # Output: (0, 1, 2, 3, 4)

output

[0, 1, 2, 3, 4]