Python | Ways to iterate tuple list of lists
Last Updated :
18 Apr, 2023
List is an important container and used almost in every code of day-day programming as well as web-development, more it is used, more is the requirement to master it and hence knowledge of its operations is necessary.
Given a tuple list of lists, write a Python program to iterate through it and get all elements.
Method #1: Use itertools.ziplongest
Python3
from itertools import zip_longest
test_list = [
[( '11' ), ( '12' ), ( '13' )],
[( '21' ), ( '22' ), ( '23' )],
[( '31' ), ( '32' ), ( '33' )]
]
print ("Initial List = ", test_list)
res_list = [item for my_list in zip_longest( * test_list)
for item in my_list if item]
print ("Resultant List = ", res_list)
|
Output:
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List = ['11', '21', '31', '12', '22', '32', '13', '23', '33']
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method#2: Using lambda + itertools.chain + zip_longest
Python3
from itertools import zip_longest, chain
test_list = [
[( '11' ), ( '12' ), ( '13' )],
[( '21' ), ( '22' ), ( '23' )],
[( '31' ), ( '32' ), ( '33' )]
]
print ("Initial List = ", test_list)
res_list = list ( filter ( lambda x: x, chain( * zip_longest( * test_list))))
print ("Resultant List = ", res_list)
|
Output:
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List = ['11', '21', '31', '12', '22', '32', '13', '23', '33']
Method #3: Using List Comprehension
List comprehension is an elegant way to define and create list in python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp.
Python3
test_list = [
[( '11' ), ( '12' ), ( '13' )],
[( '21' ), ( '22' ), ( '23' )],
[( '31' ), ( '32' ), ( '33' )]
]
print ("Initial List = ", test_list)
res_list = [item for list2 in test_list for item in list2]
print ("Resultant List = ", res_list)
|
Output:
Initial List = [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List = ['11', '12', '13', '21', '22', '23', '31', '32', '33']
Method#4: Using sum function
Here is another approach that you can use to iterate through a tuple list of lists:
Python3
test_list = [
[( '11' ), ( '12' ), ( '13' )],
[( '21' ), ( '22' ), ( '23' )],
[( '31' ), ( '32' ), ( '33' )]
]
flat_list = sum (test_list, [])
print (flat_list)
|
Output
['11', '12', '13', '21', '22', '23', '31', '32', '33']
This approach uses the sum function to flatten the list of lists. The sum function takes an iterable and a start value (in this case, an empty list []), and returns the sum of the elements in the iterable. When applied to a list of lists, the sum function flattens the list of lists into a single list.
Time complexity: O(n) where n is the number of elements in all tuple list of lists
Auxiliary Space: O(n)
Method#4: Using map and lambda:
This code is using the itertools module and the map function to iterate through a tuple list of lists and flatten it into a single list.
The itertools.chain function is used to flatten the list by chaining the elements in the list together. The map function is used to apply the lambda function to each element in test_list. The lambda function just returns the element passed to it, which is then passed to itertools.chain to flatten the list. The resulting flattened list is then converted to a list using the list function and stored in the res_list variable.
Finally, the initial list and the resultant list are printed to the console.
Python3
import itertools
test_list = [
[( '11' ), ( '12' ), ( '13' )],
[( '21' ), ( '22' ), ( '23' )],
[( '31' ), ( '32' ), ( '33' )]
]
print ( "Initial List:" , test_list)
res_list = list (itertools.chain( * map ( lambda x: x, test_list)))
print ( "Resultant List:" , res_list)
|
Output
Initial List: [['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33']]
Resultant List: ['11', '12', '13', '21', '22', '23', '31', '32', '33']
Time complexity for all the above approaches is O(n), where n is the total number of elements in the tuple list of lists. Auxiliary space for all the above approaches is O(n), as a new list is created to store the merged elements.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...