Convert list to tuple Python
In this post, you will learn how to convert a list to tuple using the Python programming language.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements. Individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being widely used. Lists play an effective role in small projects as they consume more memory.
A tuple is a sequence. Python tuples work exactly like a list, except tuples are immutable, which means they cannot be changed in place. The tuples are written inside parentheses. The tuple length is always fixed. Tuple is easy to debug, even in major projects. It consumes less memory.
Both lists and tuples are widely used to store data in Python. The difference between a tuple and a list is that a tuple stores heterogeneous data while a list stores homogeneous data. During the development process, we may come across a situation where we need to convert a list to a tuple. Here, we have mentioned different ways to convert a list to a tuple.
Convert list to tuple using built-in function tuple()
Python provides a built-in function tuple() that takes any iterable as an argument and converts it into a tuple object. In the given example, we have passed the entire list as a parameter within the tuple() function. It will return the tuple data type as an output.
x_list = ['pizza','burger','cookies','milkshake']
#convert list into tuple
x_tuple = tuple(x_list)
print(x_tuple)
print(type(x_tuple))
Output of the above code:
('pizza', 'burger', 'cookies', 'milkshake')
<class 'tuple'>
Convert list to tuple using loop inside the tuple
We can use a loop inside the built-in function tuple() to convert a Python list into a tuple object. It is the least used approach for type conversion in comparison to others.
x_list = ['milk','salt','sugar','curd']
#convert list into tuple
x_tuple = tuple(i for i in x_list )
print(x_tuple)
print(type(x_tuple))
Output of the above code:
('milk', 'salt', 'sugar', 'curd')
<class 'tuple'>
Convert list to tuple using Unpack list (*list, )
We can unpack the list elements inside the parenthesis to convert a list to a tuple. It unpacks the elements inside the tuple literal in the presence of a single comma(,). The following example demonstrates this-
x_list = ['milk','salt','sugar','curd']
#unpack list items and form tuple
x_tuple = (*x_list,)
print(x_tuple)
print(type(x_tuple))
Output of the above code:
('milk', 'salt', 'sugar', 'curd')
<class 'tuple'>
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary