Python: merge nested lists

后端 未结 3 1200
天涯浪人
天涯浪人 2020-12-03 12:57

beginner to python here.

I have 2 nested lists that I want to merge:

list1 = [\'a\',
         (b, c),
         (d, e),
         (f, g, h) ]

list2 =         


        
3条回答
  •  青春惊慌失措
    2020-12-03 13:47

    Use the power of the zip function and list comprehensions:

    list1 = [('a', ),
            ('b', 'c'),
            ('d', 'e'),
            ('f', 'g', 'h') ]
    
    list2 = [('p', 'q'),
            ('r', 's'),
            ('t', ),
            ('u', 'v', 'w') ]
    
    print [a + b for a, b in zip(list1, list2)]
    

提交回复
热议问题