0

I want to iterate through a list that has a lot of dictionaries inside it. The json response I'm trying to iterate looks something like this:

user 1 JSON response:
[
 {
 "id": "333",
 "name": "hello"
 },
 {
 "id": "999",
 "name": "hi"
 },
 {
 "id": "666",
 "name": "abc"
 },
]

user 2 JSON response:
[
 {
 "id": "555",
 "name": "hello"
 },
 {
 "id": "1001",
 "name": "hi"
 },
 {
 "id": "26236",
 "name": "abc"
 },
]

This is not the actual JSON response but it is structured the same way. What I'm trying to do is to find a specific id and store it in a variable. The JSON response I'm trying to iterate is not organized and changes every time depending on the user. So I need to find the specific id which would be easy but there are many dictionaries inside the list. I tried iterating like this:

    for guild_info in guilds:
        for guild_ids in guild_info: 

This returns the first dictionary which is id: 333. For example, I want to find the value 666 and store it in a variable. How would I do that?

PRA7H1K
  • 121
  • 9
  • You say you need to "find a specific `id` and store it in a variable". Presumably you already know the `id`, which is what you're searching for. Are you looking to assign a variable to the corresponding `name`? Also, do you need to loop through multiple user lists, or only one at a time? (In other words, each user is a list of dictionaries. Do you in fact have a list of lists of dictionaries?) – CrazyChucky Apr 11 '21 at 16:03

3 Answers3

1

What you have is a list of dictionaries.

When you run for guild_info in guilds: you will iterate through dictionaries, so here each guild_info will be a dictionary. Therefore simply take the key id like so: guild_info['id'].

If what you want to do is find the name corresponding to a specific id, you can use list comprehension and take its first element, as follows:

name = [x['name'] for x in guilds if x['id'] == '666'][0]
tlouarn
  • 161
  • 5
  • Only downside of this list comprehension is that it will keep searching through all the user's guilds even after it's found the match. Probably nothing to worry about though, performance-wise, unless you've got a whole bunch of users and/or guilds to process. – CrazyChucky Apr 11 '21 at 16:18
  • It will also throw an `IndexError` in the event that there was no match. – CrazyChucky Apr 11 '21 at 16:23
  • Based on [this answer](https://stackoverflow.com/a/2364277/12975140), your solution can be slightly modified to avoid both issues: `name = next((x['name'] for x in guilds if x['id'] == '666'), None)`. Or if it's certain there will always be a match, that can be simplified to `name = next(x['name'] for x in guilds if x['id'] == '666')`. – CrazyChucky Apr 11 '21 at 17:10
1

Here's a function that will search only until it finds the matching id and then return, which avoids checking further entries unnecessarily.

def get_name_for_id(user, id_to_find):
    # user is a list, and each guild in it is a dictionary.
    for guild in user:
        if guild['id'] == id_to_find:
            # Once the matching id is found, we're done.
            return guild['name']

    # If the loop completes without returning, then there was no match.
    return None

user = [
    {
        "id": "333",
        "name": "hello"
    },
    {
        "id": "999",
        "name": "hi"
    },
    {
        "id": "666",
        "name": "abc"
    },
]

name = get_name_for_id(user, '666')
print(name)
name2 = get_name_for_id(user, '10000')
print(name2)

Output:

abc
None
CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
0

This will create a loop which will iterate to the list of dictionaries.If you are looking for simple approach

for every_dictionary in List_of_dictionary:
    for every_dictionary_item in every_dictionary.keys():
        print(every_dictionary[every_dictionary_item])
Sanket Wagh
  • 156
  • 1
  • 14