python

Python – Convert a list of tuples into list

In this tutorial, we are going to see different ways to convert a list of tuples into a list in Python. Python provides different types of variables for programmers. We can use data types like int, float, string, list, set… in our applications. When using different types of variables, it may be necessary to convert these to different types.
 
Example:

Input: [(1, 2), (3, 4), (5, 6)]
Output: [1, 2, 3, 4, 5, 6]

 

 

Convert a list of tuples into list using comprehension list
list_tuple = [('Welcome', 1), ('To', 2), ('StackHowTo', 3)] 
# using comprehension list
mylist = [x for elem in list_tuple for x in elem] 
# Print the list
print(mylist)

Output:

['Welcome', 1, 'To', 2, 'StackHowTo', 3]

 

Convert a list of tuples into list using for loop
list_tuple = [('Welcome', 1), ('To', 2), ('StackHowTo', 3)] 

mylist = [] 
# list initialization
for tupl in list_tuple: 
    for i in tupl: 
        mylist.append(i) 
  
# Print the list
print(mylist)

Output:

['Welcome', 1, 'To', 2, 'StackHowTo', 3]
 

Convert a list of tuples into list using sum() function
list_tuple = [('Welcome', 1), ('To', 2), ('StackHowTo', 3)] 

mylist = list(sum(list_tuple, ())) 

# Print the list
print(mylist)

Output:

['Welcome', 1, 'To', 2, 'StackHowTo', 3]
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *