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.
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.
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
You can convert a tuple to a list using the list()
constructor or the *
unpacking method.
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.
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.
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.
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
- Convert Tuple to String in Python
- Python Tuple Comparison
- Convert List to Tuple in Python
- Tuple as Dictionary Key in Python
- Append Element to a Tuple in Python
- Convert Tuples to Dictionary in Python
- Flatten List of Tuples to String in Python
- Convert List of Tuples into List in Python
- Use For Loop to Iterate Tuple in Python
- Convert String Tuples to List Tuples Python
- Python Catch Multiple Exceptions
- Python Return Tuple from Function
- Python Tuple index() Method