You are currently viewing Convert Tuple to List in Python

How to convert a tuple to a list in python? You can convert a tuple into a list in python by using the list() built-in function. This function takes the iterable (set, tuple, etc.) as an argument and returns a list. list() returns a new list generated from the elements of the given tuple.

Advertisements

You can convert a tuple to a list in python by using many ways, for example, using the list(), * unpacking. In this article, I will explain convert a tuple to a list by using all these methods with examples.

1. Quick Examples of Convert Tuple to List

If you are in a hurry, below are some quick examples of how to convert a tuple to a list in Python.


# Quick examples of convert tuple to list

# Example 1: Convert the tuple to a list
tuples = (0, 2, 4, 6, 8)
mylist = list(tuples)

# Example 2: Convert the tuple to a list 
# Using * unpacking method
tuples = ('Spark', 'Python', 'pandas', 'Java')
list1 = [*tuples,]

2. Convert the Tuple to a List

You can convert a tuple to a list using the list() function. The list() function is a built-in function in Python that takes any iterable object as an argument and returns a new list object containing the same elements as the iterable object. Since tuple is iterable, you can use this to convert the tuple to a list. The resulting list is a mutable sequence, meaning you can add, remove, or modify elements as required.

In the below example, list(tuples) creates a new list containing the elements of the original tuple tuples. After conversion, you can perform list operations on mylist as needed.


# Initialize tuple
tuples = (0, 2, 4, 6, 8)
print("Original tuple:",tuples)

# Convert the tuple to a list
mylist = list(tuples)
print("Converting tuple to list:",mylist)
print("Type",type(mylist))

Yields below output.

python convert tuple list

3. Convert the Tuple to a List Using * unpacking Method

You can use unpack a tuple and assign its elements to a list by enclosing the tuple in square brackets [] and separating the elements with commas. For example, you are using the * operator to unpack the elements of the tuple tuples and assign them to the list list1. The resulting list contains the same elements as the original tuple, in the same order.


# Initialize tuple
tuples = ('Spark', 'Python', 'pandas', 'Java')
print("Original tuple:",tuples)

# Convert the tuple to a list 
# Using * unpacking method
list1 = [*tuples,]
print("Converting tuple to list:",list1)
print("Type",type(list1))

# Output:
# Original tuple: ('Spark', 'Python', 'pandas', 'Java')
# Converting tuple to list: ['Spark', 'Python', 'pandas', 'Java']
# Type <class 'list'>

Frequently Asked Questions on Convert Tuple to List

How do I convert a tuple to a list in Python?

You can convert a tuple to a list using the list() constructor or the * unpacking method.

Can I modify a tuple after converting it to a list?

You can modify a list after converting a tuple to a list. Lists are mutable, meaning you can change, add, or remove elements. However, keep in mind that tuples are immutable, so if the original tuple contained mutable objects (like lists), those objects can be modified even after conversion.

Are there any performance considerations when converting a tuple to a list?

Converting a tuple to a list involves creating a new list with the same elements. This operation has a time and space complexity proportional to the size of the tuple. For very large tuples, this operation may impact performance, so it’s worth considering whether the conversion is necessary.

Is it possible to convert a list back to a tuple?

It is possible to convert a list back to a tuple in Python. You can use the tuple() constructor to create a new tuple with the elements of the list.

Can I convert a tuple with nested structures to a list?

You can convert a tuple with nested structures to a list. The conversion applies recursively to all nested structures within the tuple.

Conclusion

In this article, I have explained how to convert a tuple to a list in python by using the list(), * unpacking with examples.

Happy Learning !!

Related Articles