Skip to content Skip to sidebar Skip to footer

Python Getting Dictionary Values In Lists

Been banging my head on desk! I have an url that is downloaded in json, then parsed_json = json.loads(response_body) to python dictionary. The issue is that data(embedded in dicts

Solution 1:

This should work:

forindex in range(parsed_json['count']):
    print(parsed_json['list'][index]['value'])

Or simpler:

for item in parsed_json['list']:
    print(item['value'])

You can print all key-value pairs by iterating over items()

for entry in parsed_json['list']:
    for key, value in entry.items():
        print(key)
        print('    ', value)

In Python 2 write print ' ', value because print is a function in Python 3 but still a statement in Python 2. If you are new to Python, start with Python 3. Python 2 is the legacy Python.

Solution 2:

If I understand correctly, you just want one list with all the values in the dicts. If what you meant is something else, let me know so I can give you another answer:

lst = []
fordictin parsed_json:
  for key indict:
      lst.append(dict[key])

Post a Comment for "Python Getting Dictionary Values In Lists"