Sebastian Bassi wrote: > Hello, could you do it for an indefinite number of elements? You did > it for a fixed (2) number of elements. I wonder if this could be done > for all members in a dictionary.
What is unclear here is in what order the keys should be visited. The following assumes that the keys should be considered in alphanumeric order. >>> def combine(d): if not d: return [] elif len(d)==1: return d.values()[0] else: keys = d.keys() keys.sort() leftKeys = keys[0:len(keys)//2] rightKeys = keys[len(keys)//2:] leftDict = dict((key,d[key]) for key in leftKeys) rightDict = dict((key,d[key]) for key in rightKeys) return [x+y for x in combine(leftDict) for y in combine(rightDict)] >>> A={'field1':['a','A'],'field2':['b','B'],'field3':['c','C'],'field4':['d','D']} >>> combine(A) ['abcd', 'abcD', 'abCd', 'abCD', 'aBcd', 'aBcD', 'aBCd', 'aBCD', 'Abcd', 'AbcD', 'AbCd', 'AbCD', 'ABcd', 'ABcD', 'ABCd', 'ABCD'] HTH again /MiO -- http://mail.python.org/mailman/listinfo/python-list