J. Doe J. Doe - 3 days ago 6
Python Question

Iterating over each element of a list inside of a dictionary

I have a dictionary, and have assigned multiple values to it like so -

d = {"names[]": ["System/CPU/User/percent", "System/CPU/System/percent"], "values[]": "average_value"}


I want the output to look like this -

names[]: System/CPU/User/percent
names[]: System/CPU/System/percent
values[]: average_value


How can I accomplish this? The different for loop iterations that I have tried are not correctly parsing through the list.

Thanks.

Answer
In [2]: for k, v in d.iteritems():
   ...:     if isinstance(v, list):
   ...:         for s in v:
   ...:             print '{}: {}'.format(k, s)
   ...:     else:
   ...:         print '{}: {}'.format(k, v)
   ...:
values[]: average_value
names[]: System/CPU/User/percent
names[]: System/CPU/System/percent
Comments