0

I have a dictionary like the one bellow:

d = {
    "00068693": [
        {"LABP": "022012"},
        {"LAOS": "022012"},
        {"LAOS": "022012"},
        {"LAOS": "022012"},
        {"LAOS": "022012"},
        {"LAOS": "022012"},
        {"LABC": "022012"},
        {"LACL": "022012"},
        {"LACL": "022012"},
        {"LACL": "022012"},
        {"LACL": "349309"},
    ],
    "00084737": [
        {"LABP": "022012"},
        {"LAOS": "022012"},
        {"LABC": "022012"},
        {"LACL": "022012"},
    ]
}

The goal of my code is to check if an ID (ex.: "00068693") has a unique code (ex.: "022012") on all files (ex.: "LAOS").

So the output for this example should be:

ID: "00068693" has different codes.
ID: "00084737" has a unique code.

But, how do I check this?

Because I don't know what files the ID has, so I can't access through each key of the list.

I'm not trying to compare the elements, just the values for each dict, but each one of them are on a different list index and I don't know the keys.

  • 2
    `dict.values()` will return a sequence of all the values in a dictionary, so you don't need to know the keys. – Barmar Jan 14 '21 at 16:43
  • Does this answer your question? [Check if all elements in a list are identical](https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical). Which list? `[list(d.values())[0] for d in files]` – Tomerikoo Jan 14 '21 at 16:44
  • Do all the nested dictionaries have just one element? That will simplify getting a list of all the values. – Barmar Jan 14 '21 at 16:47
  • Yes, I tried that, but it returns a list like this: [ dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["022012"]), dict_values(["349309"]), ] – Rodrigo Monney Jan 14 '21 at 16:47
  • `[d.values()[0] for d in list_of_dicts]` – Barmar Jan 14 '21 at 16:48
  • @Barmar `'dict_values' object is not subscriptable` – Tomerikoo Jan 14 '21 at 16:53
  • @Barmar yes, all the cases follow the exact model of the question. – Rodrigo Monney Jan 14 '21 at 16:56
  • Are you getting this structure from somewhere or generating it? It would make more sense to be a single dict instead of a list of dicts of single values. If it were `"00068693": {"LABP": "022012", "LAOS": "022012", ...}` It would be as simple as `codes = d.values()` – Tomerikoo Jan 14 '21 at 16:57
  • Thank you, @Tomerikoo, I'll make the changes and test it. – Rodrigo Monney Jan 15 '21 at 17:49

2 Answers2

0

You can Check if all elements in a list are identical. The question is which list? Well, you need to construct a list of the codes of all files. We don't really care what the files are, just their codes:

for ID, files_list in d.items():
    codes = [list(file_dict.values())[0] for file_dict in files_list]
    print(codes)
    print(f"The codes for ID {ID} are all equal? {all_equal(codes)}")

With your example dict, this is the result:

['022012', '022012', '022012', '022012', '022012', '022012', '022012', '022012', '022012', '022012', '349309']
The codes for ID 00068693 are all equal? False
['022012', '022012', '022012', '022012']
The codes for ID 00084737 are all equal? True

* all_equal above is any of the options in the accepted answer of the duplicate

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

Assuming your dictionary is called file_codes, this is how you get the results:

for k, v in file_codes.items():
    if len(set([i[j] for i in v for j in i])) != 1:
            print(f"IDS: {k} has different codes")
saedx1
  • 984
  • 6
  • 20