Mechatrnk
Mechatrnk

Reputation: 111

Making a product of an items in dictionaries

let's say I have this list of states:

states1 = ['one', 'two', 'four']
states2 = ['far away', 'normal', 'to close']
states3 = ['thor', 'iron man']

and I want to make a product of them

import itertools

state_indexes = [s for s in itertools.product(states1, states2, states3)]
print(state_indexes)

"""
[('one', 'far away', 'thor'), ('one', 'far away', 'iron man'), ('one', 'normal', 'thor'), ('one', 'normal', 'iron man'), ('one', 'to close', 'thor'), ('one', 'to close', 'iron man'), ('two', 'far away', 'thor'), ('two', 'far away', 'iron man'), ('two', 'normal', 'thor'), ('two', 'normal', 'iron man'), ('two', 'to close', 'thor'), ('two', 'to close', 'iron man'), ('four', 'far away', 'thor'), ('four', 'far away', 'iron man'), ('four', 'normal', 'thor'), ('four', 'normal', 'iron man'), ('four', 'to close', 'thor'), ('four', 'to close', 'iron 
man')]
"""

but this is hard-coded way of doing so, I want to create the list of states automatically so I created this dictionary:

my_dictionary = {}

for i in range(10):
    my_dictionary[f"item_{i}"] = ['state1', 'state2']

"""
{'item_0': ['state1', 'state2'], 'item_1': ['state1', 'state2'], 'item_2': ['state1', 'state2'], 'item_3': ['state1', 'state2'], 'item_4': ['state1', 'state2'], 'item_5': ['state1', 'state2'], 'item_6': ['state1', 'state2'], 'item_7': ['state1', 'state2'], 'item_8': ['state1', 'state2'], 'item_9': ['state1', 'state2']}
"""

Until now it works, but how can I recreate the state_indexes list?

This is one of the approaches I tried:

state_indexes = [s for s in itertools.product(my_dictionary)]
print(state_indexes)
"""
[('item_0',), ('item_1',), ('item_2',), ('item_3',), ('item_4',), ('item_5',), ('item_6',), ('item_7',), ('item_8',), ('item_9',)]
"""

How can I fix that? Thank you very much

EDIT: Expected output is basically this (as stated in the example above):

"""
[('one', 'far away', 'thor'), ('one', 'far away', 'iron man'), ('one', 'normal', 'thor'), ('one', 'normal', 'iron man'), ('one', 'to close', 'thor'), ('one', 'to close', 'iron man'), ('two', 'far away', 'thor'), ('two', 'far away', 'iron man'), ('two', 'normal', 'thor'), ('two', 'normal', 'iron man'), ('two', 'to close', 'thor'), ('two', 'to close', 'iron man'), ('four', 'far away', 'thor'), ('four', 'far away', 'iron man'), ('four', 'normal', 'thor'), ('four', 'normal', 'iron man'), ('four', 'to close', 'thor'), ('four', 'to close', 'iron 
man')]
"""

So it would look like this (I know that it is not very intuitive, that's why I used the example)

"""
[('state1', 'state1', 'state1',...,'state1'), ('state1', 'state1',..., 'state2'),..., ('state2', 'state2',... ,'state2')]
"""

Upvotes: 0

Views: 98

Answers (1)

user2390182
user2390182

Reputation: 73470

For the product of all the lists of states, you would have to unpack the dict's values, using the * operator:

state_indexes = [s for s in itertools.product(*my_dictionary.values())]
# state_indexes = list(itertools.product(*my_dictionary.values()))

Note, however, that the order of the dict values is not guaranteed in all Python versions. You might have to use a list or an collections.OrderedDict.

Upvotes: 2

Related Questions