How to combine two nested lists into a tuple list with Python

2 Answers

0 votes
lst1 = [[1, 2, 3, 4], [5, 6], [7]] 
lst2 = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g']] 

lst_tpl = [(a, b) for x, y in zip(lst1, lst2) for a, b in zip(x, y)] 
  
print(lst_tpl) 
  
  
  
'''
run:
  
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g')]
  
'''

 



answered Dec 24, 2019 by avibootz
0 votes
from itertools import chain 

lst1 = [[1, 2, 3, 4], [5, 6], [7]] 
lst2 = [['a', 'b', 'c', 'd'], ['e', 'f'], ['g']] 

lst_tpl = list(zip(chain.from_iterable(lst1), chain.from_iterable(lst2))) 
  
print(lst_tpl) 
  
  
  
'''
run:
  
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g')]
  
'''

 



answered Dec 24, 2019 by avibootz
...