How to Create Nested Dictionary in Python with 3 lists

后端 未结 7 2040
广开言路
广开言路 2020-12-03 19:31

Let us Suppose, I have created 3 lists and I want to create a dictionary for it. e.g.

a= [\'A\', \'B\', \'C\', \'D\']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
         


        
7条回答
  •  情话喂你
    2020-12-03 19:56

    Assuming what you want is to have a be keys in the outer dictionary, and b and c the key and value element of the inner dicts:

    d = {k: {x: y} for k, x, y in zip(a, b, c)}
    

    Update: However, in your example x and y are strings, so if that's what you want:

    d = {k: {str(x): str(y)} for k, x, y in zip(a, b, c)}
    

提交回复
热议问题