How to Convert Python Tuples to Lists

33

This tutorial will discuss how to convert Python tuples to lists. We have covered four methods using which you can use to convert tuples to lists in Python:

  • List() function
  • unpack (*) operator
  • Using list comprehension
  • Map() function

Also ReadHow to Handle String Index Out of Range Error In Python

What is a Tuple in Python?

Tuples are a one-dimensional data structure that can store multiple data type elements. Elements in the tuple are surrounded by parentheses ().

Syntax to create tuple:

Tuple=(element1,element2,..,element n)

Also ReadPython For Loop Index With Examples

What is a List in Python?

Lists are a one-dimensional data structure similar to tuples that can store multiple data type elements. Elements in the list are surrounded by square brackets [].

Syntax to create a list:

List=[element1,element2,..,element n]

Also ReadHow to Reverse an Array In Python [Flip Array]

What are the differences between Tuple and List

 Tuple List
 Tuples are immutable objects. You can’t modify tuples after creation. Lists are mutable objects. You can modify lists after creation.
 More memory efficient.  Less memory efficient.
 Higher time efficiency.  Less time efficiency.
 Tuples are surrounded by parentheses.  Lists are surrounded by square brackets.
 No built-in methods.  Built-in methods.
 Iterations are faster in tuples.  Iterations are slower in lists.

How to Convert Python Tuple to List

python tuples to lists

Now, let’s see the various Python methods to convert tuples to lists.

Method 1: Using list() function

The Python list() function creates a list object, and you can use it to convert a tuple to a list.

Syntax:

list(my_tuple)

where my_tuple is the input tuple.

Example:

We will create two tuples in this example and convert them into lists.

We will create one tuple with integer elements and another with string elements. After that, we will convert these integer and string tuples to lists.

Also ReadHow to Check If Dict Has Key in Python [5 Methods]

Code:

#crete a tuple with 5 integer elements
my_integer_tuple=(23,45,67,54,45)

#display
print(my_integer_tuple)

#crete a tuple with 5 string elements
my_string_tuple=("c","cpp","java","big-data","html")

#display
print(my_string_tuple)

#convert the integer tuple to list and display
print(list(my_integer_tuple))

#convert the string tuple to list and display
print(list(my_string_tuple))

Output:

(23, 45, 67, 54, 45)
('c', 'cpp', 'java', 'big-data', 'html')
[23, 45, 67, 54, 45]
['c', 'cpp', 'java', 'big-data', 'html']

If we want to display the data structure, we can use the type() function.

Syntax:

type(my_list)
type(my_tuple)

Also ReadHow to Convert Binary to Decimal in Python [5 Methods]

Example: Display the type of data structures.

Code:

#crete a tuple with 5 integer elements
my_integer_tuple=(23,45,67,54,45)

#display the type
print(type(my_integer_tuple))

#crete a tuple with 5 string elements
my_string_tuple=("c","cpp","java","big-data","html")

#display the type
print(type(my_string_tuple))

#convert the integer tuple to list and display the type
print(type(list(my_integer_tuple)))

#convert the string tuple to list and display the type
print(type(list(my_string_tuple)))

Output:

<class 'tuple'>
<class 'tuple'>
<class 'list'>
<class 'list'>

Also ReadPython Program To Display Fibonacci Series Up To N Terms

Method 2: Using unpack (*) operator

Here we use unpack operator (*) to convert Python tuple to list. We can use this operator inside the square brackets [].

Syntax:

[*my_tuple]

where my_tuple is the input tuple.

Example:

We will create two tuples in this example and convert them into lists. We will create one integer tuple and one string tuple and then use the unpack operator to convert those tuples to lists.

Code:

#crete a tuple with 5 integer elements
my_integer_tuple=(23,45,67,54,45)

#display 
print(my_integer_tuple)

#crete a tuple with 5 string elements
my_string_tuple=("c","cpp","java","big-data","html")

#display 
print(my_string_tuple)

#convert the integer tuple to list and display 
print([*my_integer_tuple])

#convert the string tuple to list and display 
print([*my_string_tuple])

Output:

(23, 45, 67, 54, 45) 
('c', 'cpp', 'java', 'big-data', 'html') 
[23, 45, 67, 54, 45] 
['c', 'cpp', 'java', 'big-data', 'html']

Also ReadPython Program To Display Multiplication Table of Any Number

Method 3: Using list comprehension

We will use list comprehension to convert tuples to lists in this method. Here you can use the list operator – [] surrounded by the comprehension.

Syntax:

[iterator  for iterator in my_tuple]

Where my_tuple is, the input tuple and the iterator will get the elements from the tuple.

Example: We will create two tuples in this example and convert them into the list.

Code:

#crete a tuple with 5 integer elements
my_integer_tuple=(23,45,67,54,45)

#display 
print(my_integer_tuple)

#crete a tuple with 5 string elements
my_string_tuple=("c","cpp","java","big-data","html")

#display 
print(my_string_tuple)

#convert the integer tuple to list and display 
print( [i for i in my_integer_tuple])

#convert the string tuple to list and display 
print( [i for i in my_string_tuple])

Output:

(23, 45, 67, 54, 45)
('c', 'cpp', 'java', 'big-data', 'html')
[23, 45, 67, 54, 45]
['c', 'cpp', 'java', 'big-data', 'html']

We can also convert a nested tuple to a list. Nested tuple means tuples inside the tuple.

Also ReadHow To Automate Google Search With Python

Structure:

tuple=((tuple1),(tuple2),.,(tuple n))

Syntax:

[list(iterator)  for iterator in my_tuple]

where my_tuple is, the input tuple and the iterator will get the elements from the tuple.

Example:

We will create two tuples in this example and convert them into a list.

Code:

#crete a nested tuple with 5 integer elements in each of the two tuples
my_integer_tuple=((23,45,67,54,45),(233,455,627,514,459))

#display 
print(my_integer_tuple)

#crete a nested tuple with 5 integer elements in each of the two tuples
my_string_tuple=(("c","cpp","java","big-data","html"),("c","cpp","java","big-data","html"))

#display 
print(my_string_tuple)

#convert the integer tuple to list and display 
print( [list(i) for i in my_integer_tuple])

#convert the string tuple to list and display 
print( [list(i) for i in my_string_tuple])

Output:

((23, 45, 67, 54, 45), (233, 455, 627, 514, 459))
(('c', 'cpp', 'java', 'big-data', 'html'), ('c', 'cpp', 'java', 'big-data', 'html'))
[[23, 45, 67, 54, 45], [233, 455, 627, 514, 459]]
[['c', 'cpp', 'java', 'big-data', 'html'], ['c', 'cpp', 'java', 'big-data', 'html']]

Also Read10 Best Udemy Python Courses for Beginners

Method 4: By using the map() function

Python Map() function applies a given function to all items in an iterable (tuple, list, etc.) and returns a map object (iterator). The best part is passing one or more iterable to the map() function.

Syntax:

list(map(list,my_tuple))

Example:

#crete a nested tuple with 5 integer elements in each of the two tuples
my_integer_tuple=((23,45,67,54,45),(233,455,627,514,459))

#display 
print(my_integer_tuple)

#crete a nested tuple with 5 integer elements in each of the two tuples
my_string_tuple=(("c","cpp","java","big-data","html"),("c","cpp","java","big-data","html"))

#display 
print(my_string_tuple)

#convert the integer tuple to list and display 
print( list(map(list,my_integer_tuple)))

#convert the string tuple to list and display 
print( list(map(list,my_string_tuple)))

Output:

((23, 45, 67, 54, 45), (233, 455, 627, 514, 459))
(('c', 'cpp', 'java', 'big-data', 'html'), ('c', 'cpp', 'java', 'big-data', 'html'))
[[23, 45, 67, 54, 45], [233, 455, 627, 514, 459]]
[['c', 'cpp', 'java', 'big-data', 'html'], ['c', 'cpp', 'java', 'big-data', 'html']]

Also Read7 Best Python IDE for Windows [Code Editors]

Wrapping Up

In this tutorial, you learned how to convert tuples to lists in Python using four different methods. Apart from that, we also explored how to convert nested tuples to lists. You can convert a tuple into a list using any of the methods mentioned above in Python.

Next Read: