Python Forum
Convert dict from list - HELP! PLEASE!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert dict from list - HELP! PLEASE!
#1
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!!!
Reply
#2
What have you tried so far?
Reply
#3
(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
Reply
#4
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]}}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,814 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,043 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,161 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,413 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Convert list to interger Clives 5 1,541 May-09-2022, 12:53 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Convert each element of a list to a string for processing tester_V 6 5,167 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 2,347 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,354 Apr-27-2021, 10:35 AM
Last Post: masterAndreas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020