I want to create new dictionary by mapping list into dictionary, so the list item will be the key and the dictionary will be the value.
Students List:
[Student1,Student2,Student3]
dictionary:
{Math:90, CS:94, Since:89}
the expected result is one dictionary:
{
"Student1":{
Math:90
},
"Student2":{
CS:94
},
"Student3":{
Since:89
}
}
I tray this:
new_dic=dic()
new_dic = dic(zip(students,dic1)
output: { Student1:Math, Student2:CS, Student3:Since }
but it give me unexpected result. also I try the solution here and it didn't works for me.
Use zip
along with some modification to reproduce the inner dict structure
names = ["Student1", "Student2", "Student3"]
values = {"Math": 90, "CS": 94, "Since": 89}
result = {name: {course: val} for name, (course, val) in zip(names, values.items())}
Alternatively, you could use:
a = ['student1','student2','student3']
b = { "Math":90 , "CS":94, "Since":89 }
c = {k1: {k2: v} for (k1,k2,v) in zip(a, b.keys(), b.values())}
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.