Skip to content
Advertisement

How to read dictionaries in list of dictionaries in python

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

JavaScript

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

JavaScript

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

JavaScript

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?

Advertisement

Answer

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:

JavaScript

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

JavaScript
Advertisement