tags:

views:

115

answers:

2
DICTA={'bw':['BW','VW'],'b':['BB','VV'],'a':['AA']}
DICTB={'yn':['$YN','$YNN'],'ye':['$YE','A$Y'],'y':['Y$']}

How to extract every possible values of that 2 Dict to

["BWYN","VWYN","BBYN","VVYN","AAYN","BWYNN","VWYNN","BBYNN","VVYNN","AAYNN",
"BWYE","VWYE","BBYE","VVYE","AAYE","ABWY","AVWY","ABBY","AVVY","AAAY",
"YBW","YVW","YBB","YVV","YAA"]

PS: Order does not matter

PPS: Not homework, but like to know how it can be implemented in efficient way.

+4  A: 

Many possible minor variants on the following fundamental theme:

print [y.replace('$', x)
  for y in (v for y in DICTB.values() for v in y)
  for x in (v for y in DICTA.values() for v in y)
]
Alex Martelli
+4  A: 

I like to go with itertools myself, but essentially the same as Alex's solution:

from itertools import product

[
    y.replace('$', x)
    for (x, y) in product(sum(DICTA.values(), []), sum(DICTB.values(), []))
]
sykora