I have a list of lists:-
[['Grade', 'V A'], ['Attendance', '16/26'], ['Session timings', '8:00 am - 8:40 am'], ['Grade', 'VII A'], ['Attendance', '24/31'], ['Session timings', '8:50 am - 9:30 am'], ['Grade', 'IX A'], ['Attendance', '20/26'], ['Session timings', '9:40 am - 10:20 am'], ['Grade', 'IX B'], ['Attendance', '21/25'], ['Session timings', '10:50 am - 11:30 am']]
And want to convert it to dictionary like this:-
{{"Grade":"V A"}, {"Attendance": "16/26"} ... }
I tried using the zip method:-
keys = []
values = []
for i in range(0, 12):
string = str(message_list[i]).replace(',',';').replace('[','').replace(']','').replace("'","").split(";")
keys.append(string[0])
values.append(string[1])
new_dict = dict(zip(keys, values))
print(new_dict)
But it only prints:-
{'Grade': ' IX B', 'Attendance': ' 21/25', 'Session timings': ' 10:50 am - 11:30 am'}
It prints only of Grade IX B, not other 3 grades. Please help me with the code...
But when I use new_dict = list(zip(keys, values))
it prints the whole as a list of tuples:
[('Grade', ' V A'), ('Attendance', ' 16/26'), ('Session timings', ' 8:00 am - 8:40 am'), ('Grade', ' VII A'), ('Attendance', ' 24/31'), ('Session timings', ' 8:50 am - 9:30 am'), ('Grade', ' IX A'), ('Attendance', ' 20/26'), ('Session timings', ' 9:40 am - 10:20 am'), ('Grade', ' IX B'), ('Attendance', ' 21/25'), ('Session timings', ' 10:50 am - 11:30 am')]
Then why doesn't it print full dictionary? Please help me with the code.
Thank you!
CodePudding user response:
You can't have the same key (e.g., 'Grade') repeated more than once in a dictionary... Keys must be unique.
You could do something like this instead:
from collections import defaultdict
d = defaultdict(list)
for k, v in lst:
d[k].append(v)
d = dict(d)
The result is:
{'Grade': ['V A', 'VII A', 'IX A', 'IX B'],
'Attendance': ['16/26', '24/31', '20/26', '21/25'],
'Session timings': ['8:00 am - 8:40 am', '8:50 am - 9:30 am', '9:40 am - 10:20 am', '10:50 am - 11:30 am']}
CodePudding user response:
The reason is you are overwriting the already existing key and hence can see only the last entry of the list
in your dict
. Another approach can be to have a nested dict
as below:
{
'Grade-V A': {
'Attendance': '16/26',
'Session timings': '8:00 am - 8:40 am'
},
'Grade-VII A': {
'Attendance': '24/31',
'Session timings': '8:50 am - 9:30 am'
},
'Grade-IX A': {
'Attendance': '20/26',
'Session timings': '9:40 am - 10:20 am'
},
'Grade-IX B': {
'Attendance': '21/25',
'Session timings': '10:50 am - 11:30 am'
}
}
with the below code:
data = [['Grade', 'V A'], ['Attendance', '16/26'], ['Session timings', '8:00 am - 8:40 am'], ['Grade', 'VII A'], ['Attendance', '24/31'], ['Session timings', '8:50 am - 9:30 am'], ['Grade', 'IX A'], ['Attendance', '20/26'], ['Session timings', '9:40 am - 10:20 am'], ['Grade', 'IX B'], ['Attendance', '21/25'], ['Session timings', '10:50 am - 11:30 am']]
output = {}
for item in data:
if item[0] == 'Grade':
grade = item[0] '-' item[1]
output[grade] = {}
else:
output[grade][item[0]] = item[1]
print (output)