0

have a list of lists such that the length of each inner list is either 1 or n (assume n > 1). For example, I have a list named ""

>>> test= [['AA', 'aa'], ['BB', 'bb'], ['CC'],['DD','dd']]

On the end I will make list like this:

[('AA', 'BB', 'CC', 'DD'), 
 ('AA', 'BB', 'CC', 'dd'), 
 ('AA', 'bb', 'CC', 'DD'), 
 ('AA', 'bb', 'CC', 'dd'), 
 ('aa', 'BB', 'CC', 'DD'), 
 ('aa', 'BB', 'CC', 'dd'), 
 ('aa', 'bb', 'CC', 'DD'), 
 ('aa', 'bb', 'CC', 'dd')]

I try to solve this by use zip, but it seems impossible... Could anyone help me? Thanks very much.

1 Answers1

2

try using itertools.product

from itertools import product
a=[['AA', 'aa'], ['BB', 'bb'], ['CC'],['DD','dd']]

for i in product(*a):
    print i

#output
('AA', 'BB', 'CC', 'DD')
('AA', 'BB', 'CC', 'dd')
('AA', 'bb', 'CC', 'DD')
('AA', 'bb', 'CC', 'dd')
('aa', 'BB', 'CC', 'DD')
('aa', 'BB', 'CC', 'dd')
('aa', 'bb', 'CC', 'DD')
('aa', 'bb', 'CC', 'dd')
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46