How to convert a list into a tuple in Python?


List and Tuple in Python are the class of data structure. The list is dynamic, whereas the tuple has static characteristics. We will understand list and tuple individually first and then understand how to convert a list into a tuple.

List

Lists are a popular and widely used data structures provided by Python. A List is a data structure in python that is mutable and has an ordered sequence of elements. Following is a list of integer values −

lis= [10,20,30,40,50]
print(lis)

If you execute the above snippet, it produces the following output −

[10, 20, 30, 40, 50]

Tuple

A tuple is a collection of python objects that are separated by commas which are ordered and immutable. Immutable implies that the tuple objects once created cannot be altered. Tuples are sequences, just like lists. The differences between tuples and lists are, that tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

tup=('tutorials', 'point', 2022,True)
print(tup)

If you execute the above snippet, produces the following output −

('tutorials', 'point', 2022, True)

In this article, we convert a list into a tuple by using a few methods. Each of these methods are discussed below.

Using the tuple() built-in function

An iterable can be passed as an input to the tuple() function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.

Example 1

In the following code, we convert a list to a tuple using the tuple() built-in function.

list_names=['Meredith', 'Kristen', 'Wright', 'Franklin']
tuple_names= tuple(list_names)
print(tuple_names)
print(type(tuple_names))

Output

('Meredith', 'Kristen', 'Wright', 'Franklin')
<class 'tuple'>

Example 2

Following is an another example of this −

my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)

Output

This will give the output −

(1, 2, 3)

Using a loop

This method is similar to the above one, but in this case, we do not send the complete list as an argument whereas we retrieve each element from the list using a for loop to send it as an argument for the tuple() built-in function.

Example

The following is an example code to convert a list to a tuple using a loop.

list_names=['Meredith', 'Kristen', 'Wright', 'Franklin']
tuple_names= tuple(i for i in list_names)
print(tuple_names)
print(type(tuple_names))

Output

('Meredith', 'Kristen', 'Wright', 'Franklin')
<class 'tuple'>

Using unpack list inside the parenthesis(*list, )

This method is faster when compared to others, but it is not efficient and difficult to understand. Inside the parenthesis, you can unpack the list elements. The list just unpacks the elements within the tuple literal, which is generated by a single comma (,).

Example

In this example code, we use the unpacking method to convert a list to a tuple.

list_names=['Meredith', 'Kristen', 'Wright', 'Franklin']
tuple_names= (*list_names,)
print(tuple_names)
print(type(tuple_names))

Output

('Meredith', 'Kristen', 'Wright', 'Franklin')
<class 'tuple'>

Updated on: 25-Oct-2023

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements