Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating over values list in Python dictionary

Hi I am looking to iterate over a Python dictionary where each key has a list of values, I am looking to either create a new list/dictionary where I have separated each value[x] or directly do stuff with the separated values.

here's a simplified example of the dictionary I have:

all_parameters = {"first": ["1a","1b","1c"], "second": ["2a","2b","2c"], "third": ["3a","3b","3c"]}

I am looking to separate the values like this (either by creating a new dictionary or list or directly doing stuff with the separated values).

grouped_parameters = [{"first": "1a", "second": "2a", "third": "3a"},
                      {"first": "1b", "second": "2b", "third": "3b"},
                      {"first": "1c", "second": "2c", "third": "3c"}]

I am insure how to iterate correctly over each key:value pair.

i = 0
for k, v in all_parameters.items():
    for item in v:
        # stuck here
    i+=1

Eventually the 'stuff' I am looking to do is convert each output (e.g. '{"first": "1a", "second": "2a", "third": "3a"}') into a string so that I can post each parameter group to a cell in a table, so ideally i'd prefer to do this dynamically instead of creating a new dictionary.

Any help would be greatly appreciated.

like image 878
MikG Avatar asked Dec 12 '25 13:12

MikG


2 Answers

Assuming all lists have the same length:

>>> length = len(next(all_parameters.itervalues()))
>>> [{k:v[i] for k,v in all_parameters.iteritems()} for i in range(length)]
[{'second': '2a', 'third': '3a', 'first': '1a'}, {'second': '2b', 'third': '3b', 'first': '1b'}, {'second': '2c', 'third': '3c', 'first': '1c'}]

In Python 3, use len(next(iter(all_parameters.values()))) and items instead of iteritems.

(The iterator shenanigans are done because you don't need a list of all the dictionary values if you only want the length of an arbitrary value-list.)

like image 140
timgeb Avatar answered Dec 15 '25 03:12

timgeb


If there's a chance of the lists being of different length, you could use map with None like so:

all_parameters = {"first": ["1a", "1b", "1c", "1d"], "second": ["2a", "2b", "2c"], "third": ["3a", "3b", "3c"]}

final = [dict(zip(all_parameters.keys(), values)) for values in map(None, *all_parameters.values())]

print final

map(None, *all_parameters.values()) gives you a tuple of the values for each key at each index - e.g. ('1a', '2a', '3a'), and by zipping this to the keys and creating a dictionary, we get the required combination.

Note: this will only work in Python 2.x as map changed in 3.x. For Python 3.x we can use itertools.zip_longest:

from itertools import zip_longest

all_parameters = {"first": ["1a", "1b", "1c", "1d"], "second": ["2a", "2b", "2c"], "third": ["3a", "3b", "3c"]}

final = [dict(zip(all_parameters.keys(), values)) for values in zip_longest(*all_parameters.values())]

print(final)

In both cases we get:

[{'second': '2a', 'third': '3a', 'first': '1a'}, {'second': '2b', 'third': '3b', 'first': '1b'}, {'second': '2c', 'third': '3c', 'first': '1c'}, {'second': None, 'third': None, 'first': '1d'}]
like image 34
asongtoruin Avatar answered Dec 15 '25 01:12

asongtoruin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!