Convert tuples into list in Python
Hello programmers, in this tutorial we will see how to convert a tuple into a list in Python.
A group of objects stored in a single variable and immutable is known as a Tuple in Python. A List in Python is also similar to a tuple and is used for data storage. The main difference between a Tuple and a List is Tuples in Python are immutable i.e. the values in it cannot be changed but a List in Python is mutable i.e. the values in it can be changed.
There are many ways to convert a tuple into a list in Python. Here we will see some of the ways to achieve the result.
Method 1 – Using a simple ‘FOR’ loop in Python
Given below is the illustration of the following:
#tuple into list def tupToList(tup): arr = [] #initialise an empty array for i in tup: #iterate over the tuple arr.append(i) return arr #driver if __name__ == "__main__": tup = (1,2,3,4,5) result = tupToList(tup) #call the function print(f"The list from the tuple {tup} is: {result}.")
Output
The list from the tuple (1, 2, 3, 4, 5) is: [1, 2, 3, 4, 5].
Explanation
Here we call the function ‘tupToList()’ with the tuple as its argument. We initialize an empty array and, using a ‘FOR’ loop, iterate through the tuple, add it to the list, and return it.
Method 2 – Using List Comprehension
Given below is the illustration of the following:
#tuple into list def tupToList(tup): return [i for i in tup] #list comprehension #driver if __name__ == "__main__": tup = (1,2,3,4,5) result = tupToList(tup) #call the function print(f"The list from the tuple {tup} is: {result}.")
Output
The list from the tuple (1, 2, 3, 4, 5) is: [1, 2, 3, 4, 5].
Explanation
We iterate the elements inside the tuple and put them in a list using the list comprehension technique.
Method 3 – Using inbuilt ‘list()’ function
Given below is the illustration of the following:
#tuple into list def tupToList(tup): return list(tup) #inbuilt function to convert tuple into list #driver if __name__ == "__main__": tup = (1,2,3,4,5) result = tupToList(tup) #call the function print(f"The list from the tuple {tup} is: {result}.")
Output
The list from the tuple (1, 2, 3, 4, 5) is: [1, 2, 3, 4, 5].
Explanation
We use the ‘list()’ in-built python function to convert the tuple to a list and return the value from the function.
Method 4 – Using ‘*’ asterisk operator
Given below is the illustration of the following:
#tuple into list def tupToList(tup): return [*tup] #unpacking * asterisk operator #driver if __name__ == "__main__": tup = (1,2,3,4,5) result = tupToList(tup) #call the function print(f"The list from the tuple {tup} is: {result}.")
Output
The list from the tuple (1, 2, 3, 4, 5) is: [1, 2, 3, 4, 5].
Explanation
We use the asterisk (*) operator to unpack the element present in the tuple and put it inside a list.
Method 5 – Using ‘map()’ method and ‘lambda’
Given below is the illustration of the following:
#tuple into list def tupToList(tup): return list(map(lambda i: i, tup)) #iterate over the tuple and map it into a list #driver if __name__ == "__main__": tup = (1,2,3,4,5) result = tupToList(tup) #call the function print(f"The list from the tuple {tup} is: {result}.")
Output
The list from the tuple (1, 2, 3, 4, 5) is: [1, 2, 3, 4, 5].
Explanation
We iterate over the tuple by using lambda and then map it correspondingly into a list.
Leave a Reply