Home > other >  How to read dictionaries in list of dictionaries in python
How to read dictionaries in list of dictionaries in python

Time:04-20

I am trying to read some JSON for a leaderboard for my discord bot, formatted like this:

{"915714438273826858": 
    [
        {"915714438823313420": 
                ["", 0]
        }, 
        [
            {"747797252105306212": 
                [1, 2]
            },
            {"695390884291674153": 
                [3, 8]
            }
        ]
    ]
}

I can read the first part fine, but reading this list here is giving me some problems:

[
    {"747797252105306212": 
        [1, 2]
    },
    {"695390884291674153": 
        [3, 8]
    }
]

How can I read the values of each of the dictionaries in the list? For example, say I want to get the dictionary with the key "747797252105306212"'s sub-list, so I could get [1,2]?

Currently, my method is to get all the keys in the larger list using

all_keys = set().union(*(d.keys() for d in lb))
all_keys = list(all_keys)

and then using the order in all_keys to call the dictionary. For example, sometimes, all_keys shows ["747797252105306212","695390884291674153"], and I can call the dictionaries using the above order, as "747797252105306212"'s index is 0 in both all_keys and in the JSON list. However, sometimes all_keys is out of order, making the program call the wrong dictionary, causing this method to be obsolete. Is there any way to fix this, or a different method?

CodePudding user response:

If I understand your issue correctly, the issue is that sets don't maintain insertion order.

You can maintain a list (which maintains order) and a separate set (for quickly checking whether you've seen a key already). For each key that you see, check whether it's a member of the set; if not, add it to the list and the seen keys set:

result = []
seen_keys = set()
for item in data:
    for key in item:
        if key not in seen_keys:
            seen_keys.add(key)
            result.append(key)
            
print(result)

With the given data, this will (deterministically!) output:

['747797252105306212', '695390884291674153']
  • Related