-2

I create a default dict in my code something like below:

defaultdict(<class 'list'>, {'month': ['JAN', 'FEB'], 'car': ['baleno', 'santro'], 'measure': ['sales', 'expense']})

cube = 'test'

Now I would like to print above dict in the below format by adding variable cube:

['month', 'JAN', 'car', 'baleno', 'measure', 'sales', 'test']

['month', 'JAN', 'car', 'baleno', 'measure','expense', 'test']

['month', 'JAN', 'car', 'santro', 'measure', 'sales', 'test']

['month', 'JAN', 'car', 'santro', 'measure', 'expense', 'test']

['month', 'FEB', 'car', 'baleno', 'measure','sales', 'test']

['month', 'FEB', 'car', 'baleno', 'measure','expense', 'test']

['month', 'FEB', 'car', 'santro', 'measure','sales', 'test']

['month', 'FEB', 'car', 'santro', 'measure','expense', 'test']

I'm actually using three loops to achieve the above output, but would like to get a neat one.

dim=['month','car','measure']
cube='test'
for b in itertools.product(*(k.values())):                                                  
        list1 = list()                                      
        for (f, c) in zip(b, dim):                                                         
            list1.append(c)                                 
            list1.append(f)                                 
        list1.append(cube)                             
        print(list1) 

k is the default dict

PS: I'm new to PYTHON. Just using it for the couple of months.

User1493
  • 481
  • 2
  • 7
  • 23

1 Answers1

0

Given the input is a dictionary, I don't think you can get much more efficient than nested for loops (note: itertools.product is equivalent to a for loop). You could possibly do it as a one liner using list comprehension, but this won't be more efficient and may be less readable.

Your implementation looks fine, here is a slightly more streamlined write up:

k = {'month': ['JAN', 'FEB'], 
     'car': ['baleno', 'santro'], 
     'measure': ['sales', 'expense']}

# Grab the keys from the dictionary as opposed to hard-coding them
dim=k.keys()
cube='test'

# cartesian product of each key's set of values
for b in itertools.product(*k.values()):                                                
    list1 = list()
    # extending empty list by (key, value) for specific values in product b                         
    for pair in zip(dim, b):                                                         
        list1.extend(pair)                                 
    list1.append(cube)                             
    print(list1) 
iacob
  • 20,084
  • 6
  • 92
  • 119
  • Thanks for optimizing the code. But, looking for something to speed up the process. That dict `k` is just a small sample I gave you. But my real code has a very big dict. The `for` loop in `itertools.product` iterates around **32616937604160** times, I'm able to parse only 150000 records per hour. – User1493 Mar 20 '19 at 11:41
  • @User1493 as I said, for calculating a cartesian product there isn't much more you can do in terms of efficiency than nested for loops. See similar implementations here: https://stackoverflow.com/questions/5228158/cartesian-product-of-a-dictionary-of-lists https://stackoverflow.com/questions/52190140/cartesian-product-of-dictionary-keys-and-values-python etc – iacob Mar 20 '19 at 11:46
  • @User1493 you may want to look into converting the dict to a dataframe and e.g. `melt` etc – iacob Mar 20 '19 at 12:34