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
# Python code to demonstrate ways to iterate # tuples list of list into single list # using itertools.ziplongest # import library from itertools import zip_longest # initialising listoflist test_list = [ [( '11' ), ( '12' ), ( '13' )], [( '21' ), ( '22' ), ( '23' )], [( '31' ), ( '32' ), ( '33' )] ] # printing initial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list res_list = [item for my_list in zip_longest( * test_list) for item in my_list if item] # print final List print ("Resultant List = ", res_list) |
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
# Python code to demonstrate # ways to iterate tuples list of list into single list # using itertools.ziplongest + lambda + chain # import library from itertools import zip_longest, chain # initialising listoflist test_list = [ [( '11' ), ( '12' ), ( '13' )], [( '21' ), ( '22' ), ( '23' )], [( '31' ), ( '32' ), ( '33' )] ] # printing initial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list # using lambda + chain + filter res_list = list ( filter ( lambda x: x, chain( * zip_longest( * test_list)))) # print final List print ("Resultant List = ", res_list) |
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
# Python code to demonstrate ways to # iterate tuples list of list into single # list using list comprehension # initialising listoflist test_list = [ [( '11' ), ( '12' ), ( '13' )], [( '21' ), ( '22' ), ( '23' )], [( '31' ), ( '32' ), ( '33' )] ] # printing initial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list # using list comprehension res_list = [item for list2 in test_list for item in list2] # print final List print ("Resultant List = ", res_list) |
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
# Initializing list of lists test_list = [ [( '11' ), ( '12' ), ( '13' )], [( '21' ), ( '22' ), ( '23' )], [( '31' ), ( '32' ), ( '33' )] ] # Flattening the list using the sum function flat_list = sum (test_list, []) # Printing the flattened list print (flat_list) #This code is contributed by Edula Vinay Kumar Reddy |
['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 import itertools # Initialize list of lists test_list = [ [( '11' ), ( '12' ), ( '13' )], [( '21' ), ( '22' ), ( '23' )], [( '31' ), ( '32' ), ( '33' )] ] # Print initial list print ( "Initial List:" , test_list) # Iterate through tuple list of lists using map and itertools.chain res_list = list (itertools.chain( * map ( lambda x: x, test_list))) # Print final list print ( "Resultant List:" , res_list) #This code is contributed by Edula Vinay Kumar Reddy |
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.