Zipping lists within a list in Python

后端 未结 1 1150
-上瘾入骨i
-上瘾入骨i 2021-01-20 22:25

I have a list of lists

big_list = [[\'a1\',\'b1\',\'c1\'], [\'a2\',\'b2\',\'c3\'], [\'a3\',\'b3\',\'c3\']]

how do I zip the lists within th

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 22:56

    Use the *args argument expansion syntax:

    zip(*big_list)
    

    The * (splash) tells Python to take each element in an iterable and apply it as a separate argument to the function.

    Demo:

    >>> big_list = [['a1','b1','c1'], ['a2','b2','c3'], ['a3','b3','c3']]
    >>> zip(*big_list)
    [('a1', 'a2', 'a3'), ('b1', 'b2', 'b3'), ('c1', 'c3', 'c3')]
    

    0 讨论(0)
提交回复
热议问题