SoFunction
Updated on 2024-11-14

Converting a Tuple to a List in Python Explained

These two Python datatypes look similar but are used differently in context. The main difference between tuples and lists is their mutability. Tuples are converted to lists only when you need to modify the elements.

(for) instance

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 a tuple and a list?

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

Converting Tuples to Lists in Python

To convert a tuple to a list you need to first make some changes and then convert the tuple to a list because tuples are immutable so you cannot change the tuple directly to a list. Now we will dive into the different ways of converting tuples to lists.

  • Using the list() function
  • Using for loops
  • Understanding using lists
  • Using the (*) operator
  • Using the map() function

Using the list() function to convert a tuple into a list

The easiest way to convert a tuple into a list is to use the built-in list() function

# Define tuples
GFG_tuple = (1, 2, 3)
# Convert tuples to lists
GFG_list = list(GFG_tuple)
print(GFG_list)

exports

[1,2,3]

Converting a tuple to a list using a for loop

Use a for loop to iterate through each element of the tuple. For each iteration of the loop (i.e., for each item in the tuple), the append() method adds the element to the end of the list.

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

exports

[1,2,3]

Converting tuples to lists using list comprehensions

Using list comprehensions is another way to perform this conversion. It helps to construct a sequence from another in a clear and concise manner.

# Define tuples
GFG_tuple = (1, 2, 3)
# Convert tuples to lists using list comprehensions
GFG_list = [element for element in GFG_tuple]
print(GFG_list)

exports

[1,2,3]

Converting a tuple to a list using the ( * ) operator

The * operator, also known as unwrapping in Python, has several different uses. One of the uses is to unwrap collections into positional arguments in function calls. We use it to convert tuples to lists.

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

exports

[1,2,3]

Using the map() function to convert a tuple to a list

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

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

exports

[1,2,3]

reach a verdict

In Python, tuple-to-list conversions can be accomplished in a number of ways. The optimized method depends on familiarity with Python's built-in functions and constructs, as well as the particular context.

Above is the tuple in Python will be converted to a list of methods explained in detail, more information about Python tuple to list please pay attention to my other related articles!