ahadcse
ahadcse

Reputation: 499

python: iterating every combination of dictionary that contains lists as value

Suppose I have a dictionary which is dictList = {1:[1,3,4,8], 2:[5,7,2,8], 3:[6,3,5,7]}

I want to print all the combinations like this: key,value first iteration 1,1 2,5 3,6 second iteration 1,1 2,5 3,3 .... and so on

Upvotes: 0

Views: 949

Answers (3)

ahadcse
ahadcse

Reputation: 499

    from itertools import product
    from itertools import izip_longest  # This is used to deal with variable length of lists

    dictList = {1:[1,3,4,8], 2:[5,7,2,8], 3:[6,3,5,7,8]}

    dict1 = [dict(izip_longest(dictList, v)) for v in product(*dictList.values())]

    for dict2 in dict1:
        for key, value in dict2.items():
            print key, value

Upvotes: 0

tenro
tenro

Reputation: 1

Like it has been said, It seems you wrongly explained your problem. But I guess you want something like :

[(1, 1), (2, 5), (3, 6), (1, 3), (2, 7), (3, 3), (1, 4), (2, 2), (3, 5), (1, 8), (2, 8), (3, 7)]

I will try to use zip. For example, zip on your values will associate all first items, all second items, etc.

WARNING : it will only work if your lists have the same length !!! (else you can import itertools.izip_longest to replace zip, the extra index will return None)

>>> zip(*dictList.values())
[(1, 5, 6), (3, 7, 3), (4, 2, 5), (8, 8, 7)]

So the following code :

for t in zip(*dictList.values()):
...    for i, k in enumerate(dictList.keys()):
...       print k, t[i]

Will print :

1 1
2 5
3 6
1 3
2 7
3 3
1 4
2 2
3 5
1 8
2 8
3 7

There is a way to do it in one line :

>>> reduce(lambda cumul, v:cumul + list(zip(dictList.keys(), v)), zip(*dictList.values()), [])
[(1, 1), (2, 5), (3, 6), (1, 3), (2, 7), (3, 3), (1, 4), (2, 2), (3, 5), (1, 8), (2, 8), (3, 7)]

Yes I know, it's not really readable, but I found it funny to try to do this kind of thing in one-line xD Take your time to understand what is happening before using it.

Hope it helps, Have a nice day.

Upvotes: 0

Kyle Strand
Kyle Strand

Reputation: 16499

Assuming that what you're trying to do is retrieve every unique set of three key,value pairs:

from itertools import permutations
# define dictList
# define numkeys to be the number of keys in dictList
# define maxLen to be the number of items in each dict value
perms = permutations(range(maxLen),numKeys)
for p in perms:
    i = 1     # you're indexing your dictionary from 1, not 0
    while i <= numKeys:
        myfunction(i,dictList[i][p[i-1]])   # ...which is why this is awkward
        i += 1

Upvotes: 1

Related Questions