Be careful that item_list
is a list not a dictionary and iterkeys()
only works on dictionaries.
Anyway, one solution is to perform the following steps:
- take each element in item_list (the elements would be dictionaries)
- get the list of values of each dictionary from above
- check the type of the value and if it is a dictionary itself, then extract the values of that dictionary and process them in any way you need
for dict_item in item_list:
for value in dict_item.values():
if type(value) == dict:
for target_value in value.values():
# do whatever you need with the target_value
print(target_value)
Make sure you also properly format your item_list (see @Kristaps comment).
[Section added after the question was updated]
I got it. So, somehow you get the dictionary embedded in a string.
In this case first thing you need to do is to convert that string to a dictionary. There are a few approaches for this. You can find interesting examples here:
I prepared a little example using ast
:
import ast
for dict_item in item_list:
for value in dict_item.values():
try:
# try to evaluate the value; the evaluation might raise some exception
res = ast.literal_eval(value)
if type(res) == dict:
# dictionary found; do whatever you need with it
print(res)
except SyntaxError:
# just continue to the next value
continue