Python Forum

Full Version: Convert dict from list - HELP! PLEASE!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I'm trying to transfer a list to a dictionnary and sort a new dict with key, but it's not success.
This is a list I need transfer:

[
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]},
{"numero":"21212121","name" : "Dupond", "first":"Alain", "notes":[11,9.5,5.5,18]},
{"numero":"21202120","name" : "Bru", "first":"Mélissa", "notes":[11,19.5,15,8]},
{"numero":"20212021","name" :"Bosse", "first":"Mélissa", "notes":[13,19.5,15,8]},
{"numero":"21202120","name" : "Allard", "first":"Chloé", "notes":[11,9.5,2,8]},
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]}
]


And this is a format of dict I need shows:

{"20202020":{"name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]},
"21212121":{"name" : "Dupond", "first":"Alain", "notes":[11,9.5,5.5,18]},
...
}


So, with this format: key is numero and value is another.

I REALLY NEED YOU HELP!!!! THANK YOU SO MUCH!!!
What have you tried so far?
(Apr-06-2020, 06:23 PM)deanhystad Wrote: [ -> ]What have you tried so far?
Hi,
Actually, It's part of my topic: I consider the list under condition that if value of numero isn't duplicated, a new dict will be issued. And I tried but I know it's not right and not okay.I just started learning Python so this is totally I can write now. And thank for your reply!

import json

def check(seq):
    new_dict = {}

    i = 0
    j = 1
    for i in seq:
        for j in seq:
            if i['numero'] == j['numero']:
                print("Numero en double!")
                print("----> ", i['numero'])
                return ("Fichier non utilisable!")
            else:
                a = str(i['name'])
                new_dict.update({i["numero"]: a})
    return new_dict
data = [
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]},
{"numero":"21212121","name" : "Dupond", "first":"Alain", "notes":[11,9.5,5.5,18]},
{"numero":"21202120","name" : "Bru", "first":"Mélissa", "notes":[11,19.5,15,8]},
{"numero":"20212021","name" :"Bosse", "first":"Mélissa", "notes":[13,19.5,15,8]},
{"numero":"21202120","name" : "Allard", "first":"Chloé", "notes":[11,9.5,2,8]},
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]}
]


new_dict = {}
for element in data:
    (key, value), *rest = element.items()
    new_dict[value] = dict(rest)

print(new_dict)
Output:
{'20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [12, 15.5, 8, 13]}, '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 8]}, '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}}
(Apr-06-2020, 06:34 PM)buran Wrote: [ -> ]
data = [
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]},
{"numero":"21212121","name" : "Dupond", "first":"Alain", "notes":[11,9.5,5.5,18]},
{"numero":"21202120","name" : "Bru", "first":"Mélissa", "notes":[11,19.5,15,8]},
{"numero":"20212021","name" :"Bosse", "first":"Mélissa", "notes":[13,19.5,15,8]},
{"numero":"21202120","name" : "Allard", "first":"Chloé", "notes":[11,9.5,2,8]},
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]}
]


new_dict = {}
for element in data:
    (key, value), *rest = element.items()
    new_dict[value] = dict(rest)

print(new_dict)
Output:
{'20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [12, 15.5, 8, 13]}, '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 8]}, '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}}

Thank you so much! And I will remember tag.