Trying to access keys and values from a nested dictionary is not working whatsoever
Question:
I’ve been trying to access the keys and values of an inner dictionary for the past couple hours and I can’t seem to be able to do it.
I’ve tried everything I know, everything I could find online, to the point where I can’t even post anything I’ve tried but even then it would be a waste of time since it’s not working.
Here’s my list of dictionaries:
tagsList = [
{
"name": "Type",
"tagValues": {
"type_tag_div1_class_name": f"{tag_values.type_tag_div1_class_name}",
"type_tag_div2_class_name": f"{tag_values.type_tag_div2_class_name}",
"type_tag_div2_jsan_name": f"{tag_values.type_tag_div2_jsan_name}",
"type_tag_div3_class_name": f"{tag_values.type_tag_div3_class_name}",
"type_tag_div3_jsan_name": f"{tag_values.type_tag_div3_jsan_name}",
"type_tag_div4_class_name": f"{tag_values.type_tag_div4_class_name}",
"type_tag_div4_jsan_name": f"{tag_values.type_tag_div4_jsan_name}"
}
},
{
"name": "Name",
"tagValues": {
"name_tag_div1_class_name": f"{tag_values.name_tag_div1_class_name}",
"name_tag_div1_jsan_name": f"{tag_values.name_tag_div1_jsan_name}",
"name_tag_h1_class_name": f"{tag_values.name_tag_h1_class_name}",
"name_tag_h1_jsan_name": f"{tag_values.name_tag_h1_jsan_name}",
"name_tag_span_class_name": f"{tag_values.name_tag_span_class_name}"
}
},
{
"name": "Address",
"tagValues": {
"address_tag_button_class_name": f"{tag_values.address_tag_button_class_name}",
"address_tag_div1_class_name": f"{tag_values.address_tag_div1_class_name}",
"address_tag_div2_class_name": f"{tag_values.address_tag_div2_class_name}",
"address_tag_div2_jsan_name": f"{tag_values.address_tag_div2_jsan_name}",
"address_tag_div3_class_name": f"{tag_values.address_tag_div3_class_name}",
"address_tag_div3_jsan_name": f"{tag_values.address_tag_div3_jsan_name}"
}
]
I’m trying to iterate over each dictionary’s tagValues
‘s keys and values.
When I tried, in multiple ways, the closest I get to be able to manipulate the keys and values is by doing this:
for dictionary in tagsList:
k = dictionary['tagValues'].keys()
v = dictionary['tagValues'].values()
for keys in k:
print(keys)
for values in v:
print(values)
Otherwise, I get a string as such:
dict_keys(['name_tag_div1_class_name', 'name_tag_div1_jsan_name', ...]): dict_values(['TIHn2', ...
or
{'mood_tag_div1_class_name': 'UmE4Qe', 'mood_tag_div2_class_name': 'C7xf8b', ... }
but this is a string.
How can I access the keys and values of this inner dictionary in order to use them ?
Answers:
You’re already iterating over the correct values. The only thing you can shorten would be creating an inner loop:
for dictionary in tagsList:
for key, item in dictionary["tagValues"].items():
print(key, item)
Here is a code example of nested dictionaries.
In your example your second nested dictionary contains NON-dictionary items.
You will not be able to iterate in a third nested loop.
nested_dictionary =
{
"dict1" : {
"value1": 1,
"value2": 2
},
"dict2" :{
"value3": 1,
"value4": 2
}
}
for outer_key, outer_value in nested_dictionary.items():
print(f"{outer_key} : {outer_value}")
for inner_key, inner_value in outer_value.items():
print(f"t{inner_key} : {inner_value}")
I’ve been trying to access the keys and values of an inner dictionary for the past couple hours and I can’t seem to be able to do it.
I’ve tried everything I know, everything I could find online, to the point where I can’t even post anything I’ve tried but even then it would be a waste of time since it’s not working.
Here’s my list of dictionaries:
tagsList = [
{
"name": "Type",
"tagValues": {
"type_tag_div1_class_name": f"{tag_values.type_tag_div1_class_name}",
"type_tag_div2_class_name": f"{tag_values.type_tag_div2_class_name}",
"type_tag_div2_jsan_name": f"{tag_values.type_tag_div2_jsan_name}",
"type_tag_div3_class_name": f"{tag_values.type_tag_div3_class_name}",
"type_tag_div3_jsan_name": f"{tag_values.type_tag_div3_jsan_name}",
"type_tag_div4_class_name": f"{tag_values.type_tag_div4_class_name}",
"type_tag_div4_jsan_name": f"{tag_values.type_tag_div4_jsan_name}"
}
},
{
"name": "Name",
"tagValues": {
"name_tag_div1_class_name": f"{tag_values.name_tag_div1_class_name}",
"name_tag_div1_jsan_name": f"{tag_values.name_tag_div1_jsan_name}",
"name_tag_h1_class_name": f"{tag_values.name_tag_h1_class_name}",
"name_tag_h1_jsan_name": f"{tag_values.name_tag_h1_jsan_name}",
"name_tag_span_class_name": f"{tag_values.name_tag_span_class_name}"
}
},
{
"name": "Address",
"tagValues": {
"address_tag_button_class_name": f"{tag_values.address_tag_button_class_name}",
"address_tag_div1_class_name": f"{tag_values.address_tag_div1_class_name}",
"address_tag_div2_class_name": f"{tag_values.address_tag_div2_class_name}",
"address_tag_div2_jsan_name": f"{tag_values.address_tag_div2_jsan_name}",
"address_tag_div3_class_name": f"{tag_values.address_tag_div3_class_name}",
"address_tag_div3_jsan_name": f"{tag_values.address_tag_div3_jsan_name}"
}
]
I’m trying to iterate over each dictionary’s tagValues
‘s keys and values.
When I tried, in multiple ways, the closest I get to be able to manipulate the keys and values is by doing this:
for dictionary in tagsList:
k = dictionary['tagValues'].keys()
v = dictionary['tagValues'].values()
for keys in k:
print(keys)
for values in v:
print(values)
Otherwise, I get a string as such:
dict_keys(['name_tag_div1_class_name', 'name_tag_div1_jsan_name', ...]): dict_values(['TIHn2', ...
or
{'mood_tag_div1_class_name': 'UmE4Qe', 'mood_tag_div2_class_name': 'C7xf8b', ... }
but this is a string.
How can I access the keys and values of this inner dictionary in order to use them ?
You’re already iterating over the correct values. The only thing you can shorten would be creating an inner loop:
for dictionary in tagsList:
for key, item in dictionary["tagValues"].items():
print(key, item)
Here is a code example of nested dictionaries.
In your example your second nested dictionary contains NON-dictionary items.
You will not be able to iterate in a third nested loop.
nested_dictionary =
{
"dict1" : {
"value1": 1,
"value2": 2
},
"dict2" :{
"value3": 1,
"value4": 2
}
}
for outer_key, outer_value in nested_dictionary.items():
print(f"{outer_key} : {outer_value}")
for inner_key, inner_value in outer_value.items():
print(f"t{inner_key} : {inner_value}")