Open In App

Convert Tuple to List in Python

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

These two Python datatypes can seem to be similar but have different usage in context. The key difference between tuples and lists lies in their mutability. Conversion of the tuple to a list only occurs when you need to modify the element.

Example

Input: GFG_Tuple = ("DSA, "MERN"," PYTHON", "KOTLIN)
Output: GFG_LIST= ["DSA, "MERN"," PYTHON", "KOTLIN]
Explanation: Here, we convert our tuple into a list with "list()" function in Python.

Difference between Tuple and List?

A tuple is immutable which means once it is created then, you cannot change its values. Tuples are defined by parentheses() and elements/items separated by commas(,) in it. Whereas, The Lists are the same as tuples but they are mutable which means that you can modify/Change the values. The list is defined by square brackets[].

Convert a Tuple to a List in Python

To convert Tuples to a List you need to first make some changes and then convert a tuple to a list as it is not possible for you to change the tuple directly into the list as Tuples are immutable. Now we will delve into the different methods to convert tuples into lists.

  • Using the list() Function
  • Using a for loop
  • Using List Comprehension
  • Using( * )operator
  • Using the map() Function

Convert a Tuple to a List using the list() Function

The simplest and easiest way to convert a tuple to a list is by using the built-in list() function.

Python3




# Define a tuple
GFG_tuple = (1, 2, 3)
 
# Convert the tuple to a list
GFG_list = list(GFG_tuple)
print(GFG_list)


Output

[1, 2, 3]

Tuple to a List using a for loop

Using for loop that iterates through each element in our tuple. For each iteration of the loop(i.e., for every item in the tuple), the append() method adds the elements to the end of the list.

Python3




GFG_tuple = ( 1, 2, 3)
GFG_list = []
 
for i in GFG_tuple:
    GFG_list.append(i)
     
print(GFG_list)


Output

[1, 2, 3]

Tuple to a List using List Comprehension

Using List comprehension is another way to perform this conversion. It helps a sequence to be built from another sequence in a clear and concise manner.

Python3




# Define a tuple
GFG_tuple = (1, 2, 3)
 
# Convert the tuple to a list using list comprehension
GFG_list = [element for element in GFG_tuple]
print(GFG_list)


Output

[1, 2, 3]

Tuple to a List using( * )operator

The * operator also known as unpacking in Python has a number of different uses. One of the use is to unpack a collection into positional arguments within a function call. We use this to convert a tuple into a list.

Python3




# Define a tuple
GFG_tuple = (1, 2, 3)
 
# Convert the tuple to a list using *operator
GFG_list = [*GFG_tuple]
 
print(GFG_list)


Output

[1, 2, 3]

Tuple to a List using the map() Function

The map() function applies a given function in each item and returns a list of results.

Python3




# Define a tuple
GFG_tuple = (1, 2, 3)
 
# Convert the tuple to a list using map function
GFG_list = list(map(lambda x: x, GFG_tuple))
 
print(GFG_list)


Output

[1, 2, 3]

Conclusion

In Python the conversion of tuples to lists can be done in many ways. The optimized method depends upon familiarity of ones in Python’s built-in functions and constructs and also on the specific context.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads