Can you convert a tuple to a list in python?

Python – Convert Tuple into List

To convert a tuple into list in Python, call list[] builtin function and pass the tuple as argument to the function. list[] returns a new list generated from the items of the given tuple.

Python list[] builtin function

In this tutorial, we will go through some examples, where we demonstrate how to convert tuple to list in Python.

Examples

Convert Tuple to List

In the following example, we initialize a tuple and convert it to list using list[sequence].

Python Program

#initialize tuple
aTuple = [True, 28, 'Tiger']

#tuple to list
aList = list[aTuple]

#print list
print[type[aList]]
print[aList]

Try Online

Output

[True, 28, 'Tiger']

Convert Tuple of Integers to List

In the following example, we initialize a tuple with all integers and convert it to a list using list[sequence].

Python Program

#initialize tuple
aTuple = [67, 28, 41, 37]

#tuple to list
aList = list[aTuple]

#print list
print[type[aList]]
print[aList]

Try Online

Output

[67, 28, 41, 37]

Conclusion

In this Python Tutorial, we learned how to convert a tuple into list in Python using list[sequence], with the help of example Python programs.

Lists and tuples are Python’s most used data types. The tuple data type in Python is similar to a list with one difference that element values of a tuple can not be modified, and tuple items are put between parentheses instead of a square bracket.

To convert the Python list to tuple, use the tuple[] method.

To convert a tuple to list in Python, use the list[] method. The list[] is a built-in Python method that takes a tuple as an argument and returns the list. The list[] takes sequence types and converts them to lists.

Syntax

list[sequence]

Parameters

The sequence is a tuple that will be converted into the list.

Return Value

The list[] method returns the list.

Example

tup = [21, 19, 11, 46, 18]
print[tup]

lt = list[tup]
print[lt]

Output

[21, 19, 11, 46, 18]
[21, 19, 11, 46, 18]

You can see that the list[] method has converted a tuple into a list, and all the tuple elements are intact.

Convert a list of tuples into a list

To convert a list of tuples into a list, use the list comprehension. List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

# List of tuple initialization
listoftuples = [["Apple", 1], ["Microsoft", 2], ["Amazon", 3]]

# Using list comprehension
out = [item for t in listoftuples for item in t]

# Printing output
print[out]

Output

['Apple', 1, 'Microsoft', 2, 'Amazon', 3]

Using list comprehension, we can convert a list of tuples into one single list.

You can also use itertools.chain[] method to convert a list of tuples into a list.

# Importing itertools
import itertools

# List of tuple initialization
tuple = [[11, 21], [31, 41], [51, 61]]

# Using itertools
out = list[itertools.chain[*tuple]]

# Printing output
print[out]

Output

[11, 21, 31, 41, 51, 61]

Converting tuple to list using for loop and append[]

Create an empty list to fill in one tuple element at a time using the append[] method and pass the tuple element into the function within a for loop body iterating over all tuple elements.

# app.py

def convert_tuple_to_list[tup]:
    brand_new_list = []
    for element in tup:
        brand_new_list.append[element]
    return brand_new_list


tupl = [1, 2, 8, 11]
print[convert_tuple_to_list[tupl]]
tupl_2 = ['Vecna', 'Bob', 'Kali', 'Eleven']
print[convert_tuple_to_list[tupl_2]]
tupl_3 = [11, ]
print[convert_tuple_to_list[tupl_3]]

Output

[1, 2, 8, 11]
['Vecna', 'Bob', 'Kali', 'Eleven']
[11]

The list append[] method appends an element to the end of the list.

Using an asterisk[*] operator

Using the unpacking asterisk operator * from Python 3.5, use the expression [*t] creates a new list with the square bracket notation and unpacks all elements of the tuple t into the list.

# app.py

def convert_tuple_to_list[tup]:
  return [*tup]


tupl = [1, 2, 8, 11]
print[convert_tuple_to_list[tupl]]
tupl_2 = ['Vecna', 'Bob', 'Kali', 'Eleven']
print[convert_tuple_to_list[tupl_2]]
tupl_3 = [11, ]
print[convert_tuple_to_list[tupl_3]]

Output

[1, 2, 8, 11]
['Vecna', 'Bob', 'Kali', 'Eleven']
[11]

 The [*tup] is the most concise one-liner to convert a tuple to a list in Python.

Conclusion

Use the list[] method to convert a tuple to a list in Python. The list[] and tuple[] return a new list and tuple when given an iterable object such as a list, tuple, set, range, etc. That is it for this tutorial.

See also

Python list to set

Python list to csv

Python list to array

Python list to string

Python list to dataframe

How do I return a tuple to a list?

To convert a tuple to list in Python, use the list[] method. The list[] is a built-in Python method that takes a tuple as an argument and returns the list. The list[] takes sequence types and converts them to lists.

How do you convert a tuple to a list of tuples in Python?

Method #1 : Using loop + * operator + items[] This is one of the ways in which this task can be performed. In this we iterate for all the keys using loop and map keys with all values by unpacking all values in tuple using * operator.

Can we convert list to tuple and tuple to list in Python?

Using the tuple[] built-in function An iterable can be passed as an input to the tuple [] function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple[] function to pass the full list as an argument, and it will return the tuple data type as an output.

Why use a tuple instead of a list?

The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified. Tuples are more memory efficient than the lists.

Chủ Đề