Home > database >  Call on data from an unnamed nested dictionary on Python
Call on data from an unnamed nested dictionary on Python

Time:12-06

I've been working on Python for some time in University, but during my time in Uni we never used dictionaries, so I'm pretty new to this data type. I apologize if what I'm asking may have an obvious answer, but I've tried searching on google with no success. So basically I need to access data inside of a nested dictionary, but the nested dictionary:

  1. Is inside a list.
  2. Doesn't have an assigned name, neither does the dictionary that it is contained in have a name.

How would I go about accessing the data inside of this list?

So here is an example of what the nested dictionary structure looks like:

{
    "key1": value1,
    "key2": value2,
    "key3": [
        {
            "subkey1": subkey_value1,
            "subkey2": subkey_value2,
            "subkey3": subkey_value3
        }
    ]
},
{
    "key1": value1,
    "key2": value2,
    "key3": [
        {
            "subkey1": subkey_value1,
            "subkey2": subkey_value2,
            "subkey3": subkey_value3
        }
    ]
},
(etc.)

So I'm trying to access the data inside of one of the subkeys (say for example "subkey2") for multiple sets of similar structures. Could anyone please guide me on how I would go about approaching this?

Here is an example of how one of my attempted approaches to seeing if I can accessing the data, that seemed to get me the closest to what I need to get:

print(["key3"][0].get("subkey2"))

CodePudding user response:

To access data inside a nested dictionary, you can use the square bracket notation.

For the example you provided, the nested dictionary is inside a list, so you can access it by using the index of the list where the nested dictionary is located. In this case, the index of the list containing the nested dictionary is 0, so you can access it using the following code:

nested_dict = data["key3"][0]

Once you have access to the nested dictionary, you can use the square bracket notation to access the data inside the nested dictionary. In this case, to access the data inside the "subkey2" key, you can use the following code:

subkey2_data = nested_dict["subkey2"]

If you want to access the data inside multiple sets of similar structures, you can use a for loop to iterate over the list containing the nested dictionaries, and then use the square bracket notation to access the data inside the nested dictionaries.

Here is an example of how you could do this:

# data is the dictionary containing the list of nested dictionaries
nested_dict_list = data["key3"]

for nested_dict in nested_dict_list:
    # Access the data inside the "subkey2" key
    subkey2_data = nested_dict["subkey2"]

Hope this helps!

CodePudding user response:

This is not possible. In python you can only address data structures that have been assigned to a variable name or at least are several layers nested in something that has been assigned to a variable. So you would need to change your code so that at least at the highest level you have a variable name.

my_list = [
    {
        "key1": value1,
        "key2": value2,
        "key3": [
            {
                "subkey1": subkey_value1,
                "subkey2": subkey_value2,
                "subkey3": subkey_value3
            }
        ]
    },
    {
        "key1": value1,
        "key2": value2,
        "key3": [
            {
                "subkey1": subkey_value1,
                "subkey2": subkey_value2,
                "subkey3": subkey_value3
            }
        ]
    }
]
print(my_list[0]["key3"][0]["subkey3"])

p.s. it might be that you are not looking at dictionary in a python list, but instead at a json object in a json array. In that case you might have to load the file or the string in which your data structure resides with json.

  • Related