0

I'm developing a function func(lvars,lconsts) that, given a list with all variables in a certain operator and a list with all constants in the current state, computes a list with all possible assignments of constants to variables.

func(['X','Y'],['a','b'])

The expected output:

[{'X':'a','Y':'a'},{'X':'b','Y':'a'},{'X':'a','Y':'b'},{'X':'b','Y':'b'}]

I tried to use itertools like this:

def func(lvars,lconsts):
    return list(itertools.product(lvars, lconsts))

but instead of the expected output im getting this:

[('X', 'a'), ('X', 'b'), ('Y', 'a'), ('Y', 'b')]
Patrick Haugh
  • 55,247
  • 13
  • 83
  • 91
Duarte Castanho
  • 175
  • 1
  • 2
  • 20
  • How about passing it into a `dict()` constructor instead of a `list()` constructor? – G. Anderson Oct 26 '18 at 18:57
  • 1
    See this identical question posted minutes ago: https://stackoverflow.com/questions/53013948/every-possible-combination-of-two-lists#comment92931442_53013948 – Patrick Haugh Oct 26 '18 at 18:58

1 Answers1

0

How about passing it into a dict() constructor instead of a list() constructor?

def func(lvars,lconsts):
    return dict(product(lvars, lconsts))
G. Anderson
  • 5,173
  • 2
  • 11
  • 19