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!
#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


Messages In This Thread
RE: Convert dict from list - HELP! PLEASE! - by cherry_cherry - Apr-06-2020, 06:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,952 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,126 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,540 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,233 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,470 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Convert list to interger Clives 5 1,645 May-09-2022, 12:53 PM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,299 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Convert each element of a list to a string for processing tester_V 6 5,378 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 2,397 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,498 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