Convert two lists into a dictionary

前端 未结 18 2499
囚心锁ツ
囚心锁ツ 2020-11-21 04:35

Imagine that you have:

keys = [\'name\', \'age\', \'food\']
values = [\'Monty\', 42, \'spam\']

What is the simplest way to produce the foll

18条回答
  •  再見小時候
    2020-11-21 04:53

    method without zip function

    l1 = [1,2,3,4,5]
    l2 = ['a','b','c','d','e']
    d1 = {}
    for l1_ in l1:
        for l2_ in l2:
            d1[l1_] = l2_
            l2.remove(l2_)
            break  
    
    print (d1)
    
    
    {1: 'd', 2: 'b', 3: 'e', 4: 'a', 5: 'c'}
    

提交回复
热议问题