0
variations = {
    'size':{'small':'Small',
            'medium':'Medium', 
            'large':'Large'}, 
    'quantity':{'20l':'20l',
                '10l':'10l',
                '5l':'5l'},
    'color':{'red':'Red',
             'blue':'Blue',
             'green':'Green'}
                }

var_list = [[i,j,k] for i in variations['color'] for j in variations['size'] for k in variations['quantity']]

You can also write the above code as:

var_list = []

for i in variations['color']:
  for j in variations['size']:
    for k in variations['quantity']:
      comb = []
      comb.append(i)
      comb.append(j)
      comb.append(k)
      Var_list.append(comb)

Both the var_list outputs:

[['red', 'small', '20l'], ['red', 'small', '10l'], ['red', 'small', '5l'], ['red', 'medium', '20l'], ['red', 'medium', '10l'], ['red', 'medium', '5l'], ['red', 'large', '20l'], ['red', 'large', '10l'], ['red', 'large', '5l'], ['blue', 'small', '20l'], ['blue', 'small', '10l'], ['blue', 'small', '5l'], ['blue', 'medium', '20l'], ['blue', 'medium', '10l'], ['blue', 'medium', '5l'], ['blue', 'large', '20l'], ['blue', 'large', '10l'], ['blue', 'large', '5l'], ['green', 'small', '20l'], ['green', 'small', '10l'], ['green', 'small', '5l'], ['green', 'medium', '20l'], ['green', 'medium', '10l'], ['green', 'medium', '5l'], ['green', 'large', '20l'], ['green', 'large', '10l'], ['green', 'large', '5l']]

var_list contains 3 for loops based on the 3 dictionaries in variations. How to write the above code so that for loops in var_list can be increased or decreased based on the number of dictionaries present in variations?

e.g if 'brand' is also present in variations, a for loop for this 'brand' should be dynamically created in the var_list, so the var_list becomes

var_list = [[i,j,k,l] for i in variations['color'] for j in variations['size'] for k in variations['quantity'] for l in varistions['brands']
 
Niiteesh
  • 73
  • 1
  • 7
  • 2
    The desired output is the cartesian product of the values of each dict. It's trivial to get the values as lists, at which point the problem is seen to be a common duplicate. (In fact, the results of `.values` on each dictionary can be used directly, even though they are not lists; `itertools.product` will accept any iterable.) – Karl Knechtel Nov 30 '22 at 19:57

1 Answers1

1

When I hear nested for loops I think the product of..

Get the product of the values of the values of variations.

variations = {
    'size':{'small':'Small',
            'medium':'Medium', 
            'large':'Large'}, 
    'quantity':{'20l':'20l',
                '10l':'10l',
                '5l':'5l'},
    'color':{'red':'Red',
             'blue':'Blue',
             'green':'Green'},
    'brand':{'one':'foo','two':'bar'}}

>>> a = [list(v.values()) for v in variations.values()]
>>> a
[['Small', 'Medium', 'Large'], ['20l', '10l', '5l'], ['Red', 'Blue', 'Green'], ['foo', 'bar']]
>>> import itertools
>>> for c in itertools.product(*a):      
...     print(c)
... 
('Small', '20l', 'Red', 'foo')
('Small', '20l', 'Red', 'bar')
('Small', '20l', 'Blue', 'foo')
('Small', '20l', 'Blue', 'bar')
('Small', '20l', 'Green', 'foo')
('Small', '20l', 'Green', 'bar')
('Small', '10l', 'Red', 'foo')
('Small', '10l', 'Red', 'bar')
('Small', '10l', 'Blue', 'foo')
('Small', '10l', 'Blue', 'bar')
('Small', '10l', 'Green', 'foo')
('Small', '10l', 'Green', 'bar')
('Small', '5l', 'Red', 'foo')
('Small', '5l', 'Red', 'bar')
('Small', '5l', 'Blue', 'foo')
('Small', '5l', 'Blue', 'bar')
('Small', '5l', 'Green', 'foo')
('Small', '5l', 'Green', 'bar')
('Medium', '20l', 'Red', 'foo')
('Medium', '20l', 'Red', 'bar')
('Medium', '20l', 'Blue', 'foo')
('Medium', '20l', 'Blue', 'bar')
('Medium', '20l', 'Green', 'foo')
('Medium', '20l', 'Green', 'bar')
('Medium', '10l', 'Red', 'foo')
('Medium', '10l', 'Red', 'bar')
('Medium', '10l', 'Blue', 'foo')
('Medium', '10l', 'Blue', 'bar')
('Medium', '10l', 'Green', 'foo')
('Medium', '10l', 'Green', 'bar')
('Medium', '5l', 'Red', 'foo')
('Medium', '5l', 'Red', 'bar')
('Medium', '5l', 'Blue', 'foo')
('Medium', '5l', 'Blue', 'bar')
('Medium', '5l', 'Green', 'foo')
('Medium', '5l', 'Green', 'bar')
('Large', '20l', 'Red', 'foo')
('Large', '20l', 'Red', 'bar')
('Large', '20l', 'Blue', 'foo')
('Large', '20l', 'Blue', 'bar')
('Large', '20l', 'Green', 'foo')
('Large', '20l', 'Green', 'bar')
('Large', '10l', 'Red', 'foo')
('Large', '10l', 'Red', 'bar')
('Large', '10l', 'Blue', 'foo')
('Large', '10l', 'Blue', 'bar')
('Large', '10l', 'Green', 'foo')
('Large', '10l', 'Green', 'bar')
('Large', '5l', 'Red', 'foo')
('Large', '5l', 'Red', 'bar')
('Large', '5l', 'Blue', 'foo')
('Large', '5l', 'Blue', 'bar')
('Large', '5l', 'Green', 'foo')
('Large', '5l', 'Green', 'bar')

>>>

Or just

>>> a = [v.values() for v in variations.values()]
>>> for c in itertools.product(*a): print(c)
wwii
  • 23,232
  • 7
  • 37
  • 77