Sunday 28 January 2024

Convert tuple to a list in Python

Approach 1: Using list() function

Using list() function, we can convert a tuple to list.

 

Example

emp = (1, 'Krishna', 'Bangalore', 34)
emp_as_list = list(emp)

 

emp_as_list = list(emp)

This line converts the emp tuple into a list. The list() function is a built-in Python function that takes an iterable (like a tuple, a set, or a dictionary) and returns a new list with the same elements. In this case, emp_as_list becomes a list containing the same elements as the emp tuple: [1, 'Krishna', 'Bangalore', 34].

>>> emp = (1, 'Krishna', 'Bangalore', 34)
>>> emp_as_list = list(emp)
>>> 
>>> emp
(1, 'Krishna', 'Bangalore', 34)
>>> type(emp)
<class 'tuple'>
>>> 
>>> 
>>> emp_as_list
[1, 'Krishna', 'Bangalore', 34]
>>> type(emp_as_list)
<class 'list'>

Approach 2: Using list comprehensions.

emp = (1, 'Krishna', 'Bangalore', 34)
emp_as_list = [item for item in emp]

emp_as_list = [item for item in emp]

This line uses a list comprehension to convert the emp tuple into a list. List comprehensions are a concise way to create lists in Python. The syntax [item for item in emp] means "create a list consisting of each item taken from the emp tuple." This process iterates over each element in the emp tuple and places each element into a new list. The result is that emp_as_list becomes a list containing the same elements as the emp tuple: [1, 'Krishna', 'Bangalore', 34].

>>> emp = (1, 'Krishna', 'Bangalore', 34)
>>> emp_as_list = [item for item in emp]
>>> 
>>> emp
(1, 'Krishna', 'Bangalore', 34)
>>> type(emp)
<class 'tuple'>
>>> 
>>> 
>>> emp_as_list
[1, 'Krishna', 'Bangalore', 34]
>>> type(emp_as_list)
<class 'list'>

Approach 3: using for loop

emp = (1, 'Krishna', 'Bangalore', 34)
emp_as_list = []
for item in emp:
  emp_as_list.append(item)

 

emp_as_list = []

This line initializes an empty list named emp_as_list. In Python, lists are mutable and ordered collections of items. This list is intended to store the elements of the emp tuple.

 

for item in emp:

Above snippet iterates over each element in the emp tuple.

 

emp_as_list.append(item):

Inside the for loop, each item from the tuple emp is appended to the list emp_as_list using the append() method. This method adds the item to the end of the list.

>>> emp = (1, 'Krishna', 'Bangalore', 34)
>>> emp_as_list = []
>>> for item in emp:
...   emp_as_list.append(item)
... 
>>> 
>>> emp
(1, 'Krishna', 'Bangalore', 34)
>>> type(emp)
<class 'tuple'>
>>> 
>>> emp_as_list
[1, 'Krishna', 'Bangalore', 34]
>>> type(emp_as_list)
<class 'list'>

 

Approach 4: Using unpacking (*) operator

emp = (1, 'Krishna', 'Bangalore', 34)
emp_as_list = [*emp]

emp_as_list = [*emp]

This line converts the emp tuple into a list. The * operator, known as the unpacking operator in Python, is used here to unpack the elements of the tuple emp. When used within the square brackets [], it effectively takes all elements from the tuple and places them into a new list.

>>> emp = (1, 'Krishna', 'Bangalore', 34)
>>> emp_as_list = [*emp]
>>> 
>>> emp
(1, 'Krishna', 'Bangalore', 34)
>>> type(emp)
<class 'tuple'>
>>> 
>>> emp_as_list
[1, 'Krishna', 'Bangalore', 34]
>>> type(emp_as_list)
<class 'list'>

Sometimes, you might want to apply some transformations on the tuple elements while adding them to a list, you can combine list with map function to achieve this.

 

hobbies_as_list = list(map(lambda x : x.upper(), hobbies))

Above statement convert each hooby in the tuple to uppercase while adding to the list.

 

lambda x : x.upper(): This is a lambda function, an anonymous (nameless) function in Python. x is the input parameter, and x.upper() is the operation performed on this parameter. x.upper() converts the string x to uppercase.

 

map(lambda x : x.upper(), hobbies): The map function applies the lambda function to each element of the hobbies tuple. The map function takes two arguments: a function and an iterable (in this case, the hobbies tuple). It applies the given function (the lambda function) to each item of the iterable and returns a map object.

 

list(...): The map object returned by the map function is then converted into a list using the list() constructor. This is necessary because map returns a map object, not a list, and the goal here is to have a list of the modified elements.

>>> hobbies = ('football', 'cricket', 'singing', 'cooking')
>>> hobbies_as_list = list(map(lambda x : x.upper(), hobbies))
>>> 
>>> hobbies
('football', 'cricket', 'singing', 'cooking')
>>> type(hobbies)
<class 'tuple'>
>>> 
>>> 
>>> hobbies_as_list
['FOOTBALL', 'CRICKET', 'SINGING', 'COOKING']
>>> type(hobbies_as_list)
<class 'list'>

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment