How do I convert between tuples and lists in Python?


First, we will see how to convert Tuple into a List in Python.

Convert Tuple with Integer Elements into a List

To convert Tuple to a List, use the list() method and set the Tuple to be converted as a parameter.

Example

Let’s see the example

# Creating a Tuple mytuple = (20, 40, 60, 80, 100) # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple)) # Tuple to list mylist = list(mytuple) # print list print("List = ",mylist) print("Type = ",type(mylist))

Output

Tuple =  (20, 40, 60, 80, 100)
Tuple Length=  5
List =  [20, 40, 60, 80, 100]
Type =  <class 'list'>

Convert Tuple with String Elements into a List

To convert Tuple to a List, use the list() method and set the Tuple with string elements to be converted as a parameter.

Example

Let’s see the example

# Creating a Tuple mytuple = ("Jacob", "Harry", "Mark", "Anthony") # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple)) # Tuple to list mylist = list(mytuple) # print list print("List = ",mylist) print("Type = ",type(mylist))

Output

Tuple =  ('Jacob', 'Harry', 'Mark', 'Anthony')
Tuple Length=  4
List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Type =  <class 'list'>

Convert List to Tuple

To convert List to Tuple, use the tuple() −

Example

# Creating a List mylist = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ",mylist) # Convert List to Tuple res = tuple(mylist) print("Tuple = ",res)

Output

List =  ['Jacob', 'Harry', 'Mark', 'Anthony']
Tuple =  ('Jacob', 'Harry', 'Mark', 'Anthony')

Updated on: 16-Sep-2022

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements