This is a quite simple problem but can have a good amount of application due to certain constraints of python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let’s discuss certain ways in which we can convert a list of tuples to list of list.
In Python, a list of tuples can be easily converted to a list of lists using a list comprehension. The basic idea is to iterate over each tuple in the list of tuples and create a new list for each tuple by converting it into a list. Then, append the new list to a new list to create a list of lists. Here's an example program that demonstrates this approach:
# Sample list of tuples list_of_tuples = [(1, 2), (3, 4), (5, 6)] # Convert list of tuples to list of lists using list comprehension list_of_lists = [list(t) for t in list_of_tuples] # Print the resulting list of lists print(list_of_lists)
In this program, we first define a sample list of tuples called list_of_tuples
. We then use a list comprehension to convert each tuple in the list to a list using the list()
function. The resulting list of lists is stored in a new variable called list_of_lists
. Finally, we print the resulting list of lists using the print()
function.
When you run this program, you should see the following output:
[[1, 2], [3, 4], [5, 6]]
As you can see, the list of tuples has been successfully converted to a list of lists using the list comprehension.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.