How to turn this:
keys_item = ['name', 'subject', 'marks']
names = ['Rony', 'Vijay', 'Imran']
subjects = ['math', 'english', 'IT']
marks = [26, 37, 25]
Into this:
students = [{'name': Rony, 'subject': 'math', 'marks': 26}, {'name': 'Vijay', 'subject': 'english', 'marks': 37}, {'name': 'Imran', 'subject': 'IT', 'marks': 25}]
Using loops in Python
CodePudding user response:
It's just a basic index iteration
students = []
for i in range(len(names)):
students.append({"name": names[i], "subject": subjects[i], "marks": marks[i]})
CodePudding user response:
Solution with zip function
students = [dict(zip(keys_item, z)) for z in zip(names, subjects, marks)]
With loops
students = []
for z in zip(names, subjects, marks):
student.append(dict(keys_item, z))
CodePudding user response:
Solution with list comprehension
print([{"name": names[i], "subject": subjects[i], "marks": marks[i]} for i in range(len(names))])