0

I have 7 iterative parameters: list_1 to list_7 the below code is static and uses 7 for-loops but I have dynamic n iterative parameters and need to use n for-loop

for a in list_1:
    for b in list_2:
        for c in list_3:
            for d in list_4:
                for e in list_5:
                    for f in list_6:
                        for g in list_7:
                            print(a,b,c,d,e,f,g)

Is there any simple solution to solve this problem using pandas or python list or numpy Note: all list contains string values

bereal
  • 32,519
  • 6
  • 58
  • 104

1 Answers1

1

Use itertools.product

import itertools
all_lists = [list_1, list_2, list_3, list_4, list_5, list_6, list_7]
product = list(itertools.product(all_lists))

example to print:

for i in itertools.product(['a', 'b', 'c'], ['1', '2', '3']):
    print(*i)

output:

a 1
a 2
a 3
b 1
b 2
...
mozway
  • 194,879
  • 13
  • 39
  • 75