Python – Convert List into Tuple

Convert List to Tuple in Python

Sometimes, because of the differences between a List and Tuple, you may need to convert a Python List to a Tuple in your application.

In this tutorial, we shall learn how to convert a list into tuple. There are many ways to do that. And we shall discuss the following methods with examples for each.

Examples

1. Convert list into tuple using tuple() builtin function

tuple() builtin function can take any iterable as argument, and return a tuple object.

In the following example, we shall take a list of strings, and convert it into a tuple.

Python Program

# Take a list of elements
list1 = ['apple', 'banana', 'cherry']

# Convert list into tuple
tuple1 = tuple(list1)

print(f'Tuple : {tuple1}')
print(type(tuple1))
Run Code

Output

Tuple : ('apple', 'banana', 'cherry')
<class 'tuple'>

You may observe in the output that we have printed the type of the variable tuple1, which is <class 'tuple'>.

2. Convert list into tuple by unpacking List inside Parenthesis

We can also unpack the items of List inside parenthesis, to create a tuple.

In the following program, we have a list, and we shall unpack the elements of this list inside parenthesis.

Python Program

# Take a list of elements
list1 = ['apple', 'banana', 'cherry']

# Unpack list items and form tuple
tuple1 = (*list1,)

print(f'Tuple : {tuple1}')
print(type(tuple1))
Run Code

Output

Tuple : ('apple', 'banana', 'cherry')
<class 'tuple'>

Summary

In this tutorial of Python Examples, we learned how to convert a Python List into Tuple in different ways, with the help of well detailed example programs.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us