Is there a way to merge multiple list index by index?

前端 未结 4 1879
粉色の甜心
粉色の甜心 2021-01-03 07:18

For example, I have three lists (of the same length)

A = [1,2,3]
B = [a,b,c]
C = [x,y,z]

and i want to merge it into something like: [[1,a,

4条回答
  •  悲哀的现实
    2021-01-03 08:02

    I was just wondering the same thing. I'm a total noob using code academy. This is what i came up to combine two lists index at index

    toppings = ['pepperoni', 'pineapple', 'cheese', 'sausage', 'olives', 'anchovies',    'mushrooms']
    
    prices = [2,6,1,3,2,7,2]
    
    num_pizzas = len(toppings)
    
    print("We sell "+str(num_pizzas)+" different kinds of pizza!")
    
    ***pizzas = list(zip(toppings, prices))***
    
    print (pizzas)
    

    the list pizzas printed out ...[('pepperoni', 2), ('pineapple', 6), ('cheese', 1), ('sausage', 3), ('olives', 2), ('anchovies', 7), ('mushrooms', 2)]

提交回复
热议问题