How to flatten two nested lists in Python

2 Answers

0 votes
from itertools import chain

def flatten_nested_list(nested_list1, nested_list2):
   return list(chain.from_iterable(nested_list1 + nested_list2))
 
nested_list1 = [[1, 2], [3]]
nested_list2 = [[4], [5, 6]]

print(flatten_nested_list(nested_list1, nested_list2))
 

 
'''
run:
 
[1, 2, 3, 4, 5, 6]

'''

 



answered Jan 25 by avibootz
0 votes
from itertools import chain

def flatten_nested_list(nested_list1, nested_list2):
   return [x for sub in (nested_list1 + nested_list2) for x in sub]
 
nested_list1 = [[1, 2], [3]]
nested_list2 = [[4], [5, 6]]

print(flatten_nested_list(nested_list1, nested_list2))
 

 
'''
run:
 
[1, 2, 3, 4, 5, 6]

'''

 



answered Jan 25 by avibootz
...