Convert a List to a Tuple in Python

Here are 3 ways to convert a list to a tuple in Python:

(1) Using the tuple() function:

my_list = ["item_1", "item_2", "item_3", ...]

my_tuple = tuple(my_list)

(2) Using tuple(i for i in my_list):

my_list = ["item_1", "item_2", "item_3", ...]

my_tuple = tuple(i for i in my_list)

(3) Using (*my_list, ):

my_list = ["item_1", "item_2", "item_3", ...]

my_tuple = (*my_list,)

Examples of Converting a List to a Tuple in Python

Example 1: Convert a list to a tuple using the tuple() function

First, create a list:

my_list = ["aa", "pp", "yy", "r", "kkk"]

print(my_list)
print(type(my_list))

You’ll notice the following list (note that “print(type(my_list))” was added to demonstrate that you got a list):

['aa', 'pp', 'yy', 'r', 'kkk']
<class 'list'>

Next, convert the list to a tuple using the tuple() function:

my_list = ["aa", "pp", "yy", "r", "kkk"]

my_tuple = tuple(my_list)

print(my_tuple)
print(type(my_tuple))

As you may see, the list was converted to a tuple as follows:

('aa', 'pp', 'yy', 'r', 'kkk')
<class 'tuple'>

Example 2: Convert a list to a tuple using tuple(i for i in my_list)

Optionally, you may convert a list to a tuple using tuple(i for i in my_list):

my_list = ["aa", "pp", "yy", "r", "kkk"]

my_tuple = tuple(i for i in my_list)

print(my_tuple)
print(type(my_tuple))

You’ll get the same tuple as before:

('aa', 'pp', 'yy', 'r', 'kkk')
<class 'tuple'>

Example 3: Convert a list to a tuple using (*my_list, )

Finally, you may convert a list to a tuple using (*my_list, ):

my_list = ["aa", "pp", "yy", "r", "kkk"]

my_tuple = (*my_list,)

print(my_tuple)
print(type(my_tuple))

The result is the same tuple:

('aa', 'pp', 'yy', 'r', 'kkk')
<class 'tuple'>

The following guide explains how to convert a tuple to a list.

Leave a Comment