Python Forum
Dict from list - HELP! PLEASE!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dict from list - HELP! PLEASE!
#1
Hi, I consider the list under condition that if value of numero isn't duplicated, a new dict will be issued from a list with key is numero and value is another.
But it's not valide in cases numero duplicated, it's alawys output a new dict. And I can't find reason.
I really need your help!!!! PLEASE AND THANK YOU!!!

[
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[15,15.5,8,13]},
{"numero":"21212121","name" : "Dupond", "first":"Alain", "notes":[11,9.5,5.5,18]},
{"numero":"28790020","name" : "Férien", "first":"Mélissa", "notes":[13,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,17]},
{"numero":"29019022","name" : "Durand", "first":"Alan", "notes":[12,15.5,8,13]}
]
 
import json
 
def check(seq):
    new_dict = {}
    for i in seq:
        for j in seq:
            if (j!=i and (i['numero'] == j['numero'])):
                print("Numero en double!\n " + "----> " + i['numero'] + " \n Fichier non utilisable!")
            else:
                (key, value), *rest = i.items()
                new_dict[value] = dict(rest)
 
    return new_dict
 
 
with open('source1bad1.json', 'r', encoding="utf-8") as liste:
    f = json.load(liste)
 
print(check(f))
Output:
{'20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [15, 15.5, 8, 13]}, '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, '28790020': {'name': 'Férien', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}, '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}, '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 17]}, '29019022': {'name': 'Durand', 'first': 'Alan', 'notes': [12, 15.5, 8, 13]}} or Numero duplicated ----> (numero)
Reply
#2
Unpack the list and check if the key value is already in the dictionary.
def check(seq):
    new_dict = {}
    for i in seq:
        (key, value), *rest = i.items()
        if value in new_dict:
            print("Numero en double! " + "---->" +
                  value + " Fichier non utilisable!")
        else:
            new_dict[value] = dict(rest)
    return new_dict
I don't know what you want to do for duplicated entries. The code above prints your message and excludes the duplicate entry.
Reply
#3
(Apr-07-2020, 09:36 AM)deanhystad Wrote: Unpack the list and check if the key value is already in the dictionary.
def check(seq):
    new_dict = {}
    for i in seq:
        (key, value), *rest = i.items()
        if value in new_dict:
            print("Numero en double! " + "---->" +
                  value + " Fichier non utilisable!")
        else:
            new_dict[value] = dict(rest)
    return new_dict
I don't know what you want to do for duplicated entries. The code above prints your message and excludes the duplicate entry.

If If the list has a number of key "numero" duplicated, it does not create a new dict. New dict is only available when all values of key "numero" are unique.
Reply
#4
How would you do that then? My example identifies a duplicate key but continues on and returns a dictionary. What change could you make to return (An empty dictionary? None?) instead of the partially constructed new_dict?
Reply
#5
(Apr-07-2020, 03:45 PM)deanhystad Wrote: How would you do that then? My example identifies a duplicate key but continues on and returns a dictionary. What change could you make to return (An empty dictionary? None?) instead of the partially constructed new_dict?
yes, it return an empty dic.
Reply
#6
I am not asking what it should do, I am asking how you would do that. How could my function be modified to return an empty dictionary when it discovers a duplicate key?
Reply
#7
(Apr-07-2020, 05:29 PM)deanhystad Wrote: I am not asking what it should do, I am asking how you would do that. How could my function be modified to return an empty dictionary when it discovers a duplicate key?

This's also my question, I don't know how can return emty dict
Reply
#8
That is not true. In your first example you created an empty dictionary and you returned a value. You know how to do both of those things, now just put them together. You are overthinking this problem. Think the most obvious answer to the question: How do I return an empty dictionary?
Reply
#9
(Apr-08-2020, 01:27 PM)deanhystad Wrote: That is not true. In your first example you created an empty dictionary and you returned a value. You know how to do both of those things, now just put them together. You are overthinking this problem. Think the most obvious answer to the question: How do I return an empty dictionary?

But when I tested my firt code, it's always return a new dict, and if it has a numero duplicated, it will delete the second duplicate value and return a new dict. And I don't know why because my condition return a new dict when 'else'.
Reply
#10
The condition in your first example does not return a new_dict when else. The else condition adds an entry to new_dict. Printing an error message does nothing to stop processing the list or stop adding items to the dictionary, and it does not return an empty dictionary.

What happens if you replace printing the error message with "return {}"?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,901 May-14-2021, 07:14 AM
Last Post: perfringo
  List index out of range when turning CSV into dict ranbarr 15 6,448 May-12-2021, 10:38 AM
Last Post: ranbarr
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,077 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  maximum and minimum element from the list and return output in dict MeeranRizvi 1 3,746 Jan-02-2017, 02:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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