简体   繁体   中英

Create a Dictionary from 2 Lists with Default Keys and the Values from the Lists

So I got 2 lists:

letters = ['a', 'b', 'c']    
nums = [1, 2, 3]

Desired results:

dict = {
'Letter': 'A',
'Number': 1,
'Letter' : 'B',
'Number': 2,
'Letter' : 'C',
'Number': 3,
}

I found many solutions for simply combining dicts or creating a dict with dicts but nothing to solve this.

What would be the way to get this result?

This is the only way to do it to get the key the same way as in your example

letters = ['a', 'b', 'c']    
nums = [1, 2, 3]

dict = {}
dict['Number'] = {}
dict['Letter'] = {}

for x in range(len(letters)):
    dict['Letter'][letters[x]] = letters[x]
    dict['Number'][nums[x]] = nums[x]

print(dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM