
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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'>
- Related Articles
- Convert a list into tuple of lists in Python
- How to convert JSON data into a Python tuple?
- How to convert python tuple into a two-dimensional table?
- How I can convert a Python Tuple into Dictionary?
- Python program to convert Set into Tuple and Tuple into Set
- Convert set into a list in Python
- How to convert a tuple into an array in C#?
- Convert a nested list into a flat list in Python
- Python – To convert a list of strings with a delimiter to a list of tuple
- How can I convert Python strings into tuple?
- Python program to convert a list of strings with a delimiter to a list of tuple
- Convert a string representation of list into list in Python
- How can I append a tuple into another tuple in Python?
- How to convert a list of lists into a single list in R?
- How can we convert a list of characters into a string in Python?
