Creating a Tuple in Python

To create a tuple in Python, you can place the values in parentheses separated by commas. A single-element tuple must include a comma after the value; otherwise, Python will create an object of the same type as the value, not a tuple. A tuple can contain any number of elements of different types. Tuples in Python are an ordered and immutable data type; they cannot be changed once created. In this Python Tuple example, we create a tuple with multiple elements. Click Execute to run the Python Tuple example online and see the result.
Creating a Tuple in Python Execute
my_tuple = ('Orange', 'Apple', 'Cherry')

print(my_tuple)
Updated: Viewed: 1826 times

How to create an empty tuple in Python?

To create an empty tuple, you need to use empty parentheses. A tuple can also be created using the tuple() constructor. Note: A tuple is an immutable data type, so you can't change it once you create it.

Python Tuple creation example
my_tuple = ()

print(type(my_tuple))
# output: <class 'tuple'>

my_tuple = tuple()

print(type(my_tuple))
# output: <class 'tuple'>

How to create a value-initialized tuple in Python?

Value-initialized tuples are created by listing the values in parentheses and separating them with commas. Tuples can contain any data type.

Python Tuple initialization example
my_tuple = ('Orange', 2, True)

print(my_tuple)
# output: ('Orange', 2, True)

How to create a Tuple with one element in Python?

To create a tuple with one element, you can put a comma after the value. You can also create a single-element tuple using the tuple constructor (in this case, the comma is not required).

Python one-element Tuple Example
my_tuple = ('Apple',)

print(type(my_tuple))
# output: <class 'tuple'>

my_tuple = tuple('Apple')

print(type(my_tuple))
# output: <class 'tuple'>

How to create a Tuple from a List in Python?

To create a tuple from a list, you need to pass the list to the tuple constructor. You can also create a tuple by unpacking the list items in parentheses using the "*" operator. In this case, you need to put a comma after your list. This way is slightly faster, but you lose the readability of your code.

An example of creating a tuple from a list in Python
my_list = ['Orange', 'Apple', 'Cherry']
my_tuple = tuple(my_list)

print(my_tuple)
# output: ('Orange', 'Apple', 'Cherry')

my_tuple = (*my_list,)

print(my_tuple)
# output: ('Orange', 'Apple', 'Cherry')

How to add new data to a Tuple in Python?

To add new data to an existing tuple, you can add a new tuple to an existing one using the "+=" operator. A tuple is an immutable data type, so this method will create a new tuple, not modify an existing one.

Python adding data to tuple example
my_tuple = ('Orange', 'Apple')

my_tuple += ('Cherry',)

print(my_tuple)
# output: ('Orange', 'Apple', 'Cherry')

How can I update Tuple data in Python?

A tuple is an immutable data type, but you can convert it to a list, make changes, and save it as a new tuple. Thus, we can perform with a tuple all the operations that Python List allows you to do.

Python Data сhange in a Tuple
my_tuple = ('Orange', 'Apple', 'Cherry')

edit_tuple = list(my_tuple)
edit_tuple.remove('Apple')
edit_tuple.append('Pear')

my_tuple = tuple(edit_tuple)

print(my_tuple)
# output: ('Orange', 'Cherry', 'Pear')

How can I get the length of a tuple?

You can find the length of a tuple using the built-in len() method, which returns the number of elements in the tuple.

Python tuple len() example
my_tuple = ('Orange', 'Apple', 'Cherry')

print(len(my_tuple))
# output: 3