Convert dict to list of dict for each combinations












0















I have a dict looks like this :



my_dict = {
"a":[1, 2, 3],
"b":[10],
"c":[4, 5],
"d":[11]
}


And I would like to obtain a list containig all combinations keeping keys and value like this:



result = [
{"a":1, "b":10, "c":4, "d":11},
{"a":1, "b":10, "c":5, "d":11},
{"a":2, "b":10, "c":4, "d":11},
{"a":2, "b":10, "c":5, "d":11},
{"a":3, "b":10, "c":4, "d":11},
{"a":3, "b":10, "c":5, "d":11}
]


Do someone have a solution for this ?
Is there any existing solution to do this, or how should I proceed to do it myself ?



Thank you.










share|improve this question

























  • I hope using itertools.combinations you can generate various combinations of a list but not sure on generating for a dictionary.

    – Sekar Ramu
    Nov 22 '18 at 9:17
















0















I have a dict looks like this :



my_dict = {
"a":[1, 2, 3],
"b":[10],
"c":[4, 5],
"d":[11]
}


And I would like to obtain a list containig all combinations keeping keys and value like this:



result = [
{"a":1, "b":10, "c":4, "d":11},
{"a":1, "b":10, "c":5, "d":11},
{"a":2, "b":10, "c":4, "d":11},
{"a":2, "b":10, "c":5, "d":11},
{"a":3, "b":10, "c":4, "d":11},
{"a":3, "b":10, "c":5, "d":11}
]


Do someone have a solution for this ?
Is there any existing solution to do this, or how should I proceed to do it myself ?



Thank you.










share|improve this question

























  • I hope using itertools.combinations you can generate various combinations of a list but not sure on generating for a dictionary.

    – Sekar Ramu
    Nov 22 '18 at 9:17














0












0








0








I have a dict looks like this :



my_dict = {
"a":[1, 2, 3],
"b":[10],
"c":[4, 5],
"d":[11]
}


And I would like to obtain a list containig all combinations keeping keys and value like this:



result = [
{"a":1, "b":10, "c":4, "d":11},
{"a":1, "b":10, "c":5, "d":11},
{"a":2, "b":10, "c":4, "d":11},
{"a":2, "b":10, "c":5, "d":11},
{"a":3, "b":10, "c":4, "d":11},
{"a":3, "b":10, "c":5, "d":11}
]


Do someone have a solution for this ?
Is there any existing solution to do this, or how should I proceed to do it myself ?



Thank you.










share|improve this question
















I have a dict looks like this :



my_dict = {
"a":[1, 2, 3],
"b":[10],
"c":[4, 5],
"d":[11]
}


And I would like to obtain a list containig all combinations keeping keys and value like this:



result = [
{"a":1, "b":10, "c":4, "d":11},
{"a":1, "b":10, "c":5, "d":11},
{"a":2, "b":10, "c":4, "d":11},
{"a":2, "b":10, "c":5, "d":11},
{"a":3, "b":10, "c":4, "d":11},
{"a":3, "b":10, "c":5, "d":11}
]


Do someone have a solution for this ?
Is there any existing solution to do this, or how should I proceed to do it myself ?



Thank you.







python list dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:23









lospejos

1,46221426




1,46221426










asked Nov 22 '18 at 9:05









FurlingsFurlings

63




63













  • I hope using itertools.combinations you can generate various combinations of a list but not sure on generating for a dictionary.

    – Sekar Ramu
    Nov 22 '18 at 9:17



















  • I hope using itertools.combinations you can generate various combinations of a list but not sure on generating for a dictionary.

    – Sekar Ramu
    Nov 22 '18 at 9:17

















I hope using itertools.combinations you can generate various combinations of a list but not sure on generating for a dictionary.

– Sekar Ramu
Nov 22 '18 at 9:17





I hope using itertools.combinations you can generate various combinations of a list but not sure on generating for a dictionary.

– Sekar Ramu
Nov 22 '18 at 9:17












5 Answers
5






active

oldest

votes


















-1














Try:



def permute(d):
k = d.keys()
perms = itertools.product(*d.values())
return [dict(zip(k, v)) for v in perms]

>>> pprint(permute(my_dict))
[{'a': 1, 'b': 10, 'c': 4, 'd': 11},
{'a': 1, 'b': 10, 'c': 5, 'd': 11},
{'a': 2, 'b': 10, 'c': 4, 'd': 11},
{'a': 2, 'b': 10, 'c': 5, 'd': 11},
{'a': 3, 'b': 10, 'c': 4, 'd': 11},
{'a': 3, 'b': 10, 'c': 5, 'd': 11}]





share|improve this answer





















  • 1





    Thank you Mateen, that works perfectly !

    – Furlings
    Nov 22 '18 at 9:30



















2














A task for itertools.product:



>>> from itertools import product
>>> for dict_items in product(*[product([k],v) for k, v in my_dict.items()]):
... print(dict(dict_items))

{'a': 1, 'b': 10, 'c': 4, 'd': 11}
{'a': 1, 'b': 10, 'c': 5, 'd': 11}
{'a': 2, 'b': 10, 'c': 4, 'd': 11}
{'a': 2, 'b': 10, 'c': 5, 'd': 11}
{'a': 3, 'b': 10, 'c': 4, 'd': 11}
{'a': 3, 'b': 10, 'c': 5, 'd': 11}


Small explanation:



The inner product(...) will expand the dict to a list such as [[(k1, v11), (k1, v12), ...], [(k2, v21), (k2, v22), ...], ...].



The outer product(...) will reassemble the items lists by choosing one tuple from each list.



dict(...) will create a dictionary from a sequence of (k1, v#), (k2, v#), ... tuples.






share|improve this answer


























  • Thanks for explanations ;)

    – Furlings
    Nov 22 '18 at 9:42



















0














Assuming that you are only interested in my_dict having 4 keys, it is simple enough to use nested for loops:



my_dict = {
"a": [1, 2, 3],
"b": [10],
"c": [4, 5],
"d": [11]
}

result =
for a_val in my_dict['a']:
for b_val in my_dict['b']:
for c_val in my_dict['c']:
for d_val in my_dict['d']:
result.append({'a': a_val, 'b': b_val, 'c': c_val, 'd': d_val})

print(result)


This gives the expected result.






share|improve this answer
























  • Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

    – Furlings
    Nov 22 '18 at 9:19











  • No problem. Then the itertools approach is definitely the right one!

    – DatHydroGuy
    Nov 22 '18 at 9:20



















0














You can use:



from itertools import product
allNames = sorted(my_dict)
values= list(product(*(my_dict[Name] for Name in allNames)))
d = list(dict(zip(['a','b','c','d'],i)) for i in values)


Output:



[{'a': 1, 'c': 4, 'b': 10, 'd': 11}, 
{'a': 1, 'c': 5, 'b': 10, 'd': 11},
{'a': 2, 'c': 4, 'b': 10, 'd': 11},
{'a': 2, 'c': 5, 'b': 10, 'd': 11},
{'a': 3, 'c': 4, 'b': 10, 'd': 11},
{'a': 3, 'c': 5, 'b': 10, 'd': 11}]





share|improve this answer































    0














    itertools.product produces the combinations of a list of iterators.



    dict.values() gets the list needed.



    For each combination, zip up the dict.keys() with the combination.



    Use a list comprehension to collect them up:



    from itertools import product
    from pprint import pprint

    my_dict = {
    "a":[1, 2, 3],
    "b":[10],
    "c":[4, 5],
    "d":[11]
    }

    result = [dict(zip(my_dict,i)) for i in product(*my_dict.values())]
    pprint(result)


    Output:



    [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
    {'a': 1, 'b': 10, 'c': 5, 'd': 11},
    {'a': 2, 'b': 10, 'c': 4, 'd': 11},
    {'a': 2, 'b': 10, 'c': 5, 'd': 11},
    {'a': 3, 'b': 10, 'c': 4, 'd': 11},
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





    share|improve this answer
























    • Thank you very much for explanation ! It's more clear now ;)

      – Furlings
      Nov 22 '18 at 9:34











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427249%2fconvert-dict-to-list-of-dict-for-each-combinations%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    Try:



    def permute(d):
    k = d.keys()
    perms = itertools.product(*d.values())
    return [dict(zip(k, v)) for v in perms]

    >>> pprint(permute(my_dict))
    [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
    {'a': 1, 'b': 10, 'c': 5, 'd': 11},
    {'a': 2, 'b': 10, 'c': 4, 'd': 11},
    {'a': 2, 'b': 10, 'c': 5, 'd': 11},
    {'a': 3, 'b': 10, 'c': 4, 'd': 11},
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





    share|improve this answer





















    • 1





      Thank you Mateen, that works perfectly !

      – Furlings
      Nov 22 '18 at 9:30
















    -1














    Try:



    def permute(d):
    k = d.keys()
    perms = itertools.product(*d.values())
    return [dict(zip(k, v)) for v in perms]

    >>> pprint(permute(my_dict))
    [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
    {'a': 1, 'b': 10, 'c': 5, 'd': 11},
    {'a': 2, 'b': 10, 'c': 4, 'd': 11},
    {'a': 2, 'b': 10, 'c': 5, 'd': 11},
    {'a': 3, 'b': 10, 'c': 4, 'd': 11},
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





    share|improve this answer





















    • 1





      Thank you Mateen, that works perfectly !

      – Furlings
      Nov 22 '18 at 9:30














    -1












    -1








    -1







    Try:



    def permute(d):
    k = d.keys()
    perms = itertools.product(*d.values())
    return [dict(zip(k, v)) for v in perms]

    >>> pprint(permute(my_dict))
    [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
    {'a': 1, 'b': 10, 'c': 5, 'd': 11},
    {'a': 2, 'b': 10, 'c': 4, 'd': 11},
    {'a': 2, 'b': 10, 'c': 5, 'd': 11},
    {'a': 3, 'b': 10, 'c': 4, 'd': 11},
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





    share|improve this answer















    Try:



    def permute(d):
    k = d.keys()
    perms = itertools.product(*d.values())
    return [dict(zip(k, v)) for v in perms]

    >>> pprint(permute(my_dict))
    [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
    {'a': 1, 'b': 10, 'c': 5, 'd': 11},
    {'a': 2, 'b': 10, 'c': 4, 'd': 11},
    {'a': 2, 'b': 10, 'c': 5, 'd': 11},
    {'a': 3, 'b': 10, 'c': 4, 'd': 11},
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 9:19

























    answered Nov 22 '18 at 9:13









    Mateen UlhaqMateen Ulhaq

    11.5k114793




    11.5k114793








    • 1





      Thank you Mateen, that works perfectly !

      – Furlings
      Nov 22 '18 at 9:30














    • 1





      Thank you Mateen, that works perfectly !

      – Furlings
      Nov 22 '18 at 9:30








    1




    1





    Thank you Mateen, that works perfectly !

    – Furlings
    Nov 22 '18 at 9:30





    Thank you Mateen, that works perfectly !

    – Furlings
    Nov 22 '18 at 9:30













    2














    A task for itertools.product:



    >>> from itertools import product
    >>> for dict_items in product(*[product([k],v) for k, v in my_dict.items()]):
    ... print(dict(dict_items))

    {'a': 1, 'b': 10, 'c': 4, 'd': 11}
    {'a': 1, 'b': 10, 'c': 5, 'd': 11}
    {'a': 2, 'b': 10, 'c': 4, 'd': 11}
    {'a': 2, 'b': 10, 'c': 5, 'd': 11}
    {'a': 3, 'b': 10, 'c': 4, 'd': 11}
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}


    Small explanation:



    The inner product(...) will expand the dict to a list such as [[(k1, v11), (k1, v12), ...], [(k2, v21), (k2, v22), ...], ...].



    The outer product(...) will reassemble the items lists by choosing one tuple from each list.



    dict(...) will create a dictionary from a sequence of (k1, v#), (k2, v#), ... tuples.






    share|improve this answer


























    • Thanks for explanations ;)

      – Furlings
      Nov 22 '18 at 9:42
















    2














    A task for itertools.product:



    >>> from itertools import product
    >>> for dict_items in product(*[product([k],v) for k, v in my_dict.items()]):
    ... print(dict(dict_items))

    {'a': 1, 'b': 10, 'c': 4, 'd': 11}
    {'a': 1, 'b': 10, 'c': 5, 'd': 11}
    {'a': 2, 'b': 10, 'c': 4, 'd': 11}
    {'a': 2, 'b': 10, 'c': 5, 'd': 11}
    {'a': 3, 'b': 10, 'c': 4, 'd': 11}
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}


    Small explanation:



    The inner product(...) will expand the dict to a list such as [[(k1, v11), (k1, v12), ...], [(k2, v21), (k2, v22), ...], ...].



    The outer product(...) will reassemble the items lists by choosing one tuple from each list.



    dict(...) will create a dictionary from a sequence of (k1, v#), (k2, v#), ... tuples.






    share|improve this answer


























    • Thanks for explanations ;)

      – Furlings
      Nov 22 '18 at 9:42














    2












    2








    2







    A task for itertools.product:



    >>> from itertools import product
    >>> for dict_items in product(*[product([k],v) for k, v in my_dict.items()]):
    ... print(dict(dict_items))

    {'a': 1, 'b': 10, 'c': 4, 'd': 11}
    {'a': 1, 'b': 10, 'c': 5, 'd': 11}
    {'a': 2, 'b': 10, 'c': 4, 'd': 11}
    {'a': 2, 'b': 10, 'c': 5, 'd': 11}
    {'a': 3, 'b': 10, 'c': 4, 'd': 11}
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}


    Small explanation:



    The inner product(...) will expand the dict to a list such as [[(k1, v11), (k1, v12), ...], [(k2, v21), (k2, v22), ...], ...].



    The outer product(...) will reassemble the items lists by choosing one tuple from each list.



    dict(...) will create a dictionary from a sequence of (k1, v#), (k2, v#), ... tuples.






    share|improve this answer















    A task for itertools.product:



    >>> from itertools import product
    >>> for dict_items in product(*[product([k],v) for k, v in my_dict.items()]):
    ... print(dict(dict_items))

    {'a': 1, 'b': 10, 'c': 4, 'd': 11}
    {'a': 1, 'b': 10, 'c': 5, 'd': 11}
    {'a': 2, 'b': 10, 'c': 4, 'd': 11}
    {'a': 2, 'b': 10, 'c': 5, 'd': 11}
    {'a': 3, 'b': 10, 'c': 4, 'd': 11}
    {'a': 3, 'b': 10, 'c': 5, 'd': 11}


    Small explanation:



    The inner product(...) will expand the dict to a list such as [[(k1, v11), (k1, v12), ...], [(k2, v21), (k2, v22), ...], ...].



    The outer product(...) will reassemble the items lists by choosing one tuple from each list.



    dict(...) will create a dictionary from a sequence of (k1, v#), (k2, v#), ... tuples.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 9:27

























    answered Nov 22 '18 at 9:18









    fferrifferri

    11.7k32251




    11.7k32251













    • Thanks for explanations ;)

      – Furlings
      Nov 22 '18 at 9:42



















    • Thanks for explanations ;)

      – Furlings
      Nov 22 '18 at 9:42

















    Thanks for explanations ;)

    – Furlings
    Nov 22 '18 at 9:42





    Thanks for explanations ;)

    – Furlings
    Nov 22 '18 at 9:42











    0














    Assuming that you are only interested in my_dict having 4 keys, it is simple enough to use nested for loops:



    my_dict = {
    "a": [1, 2, 3],
    "b": [10],
    "c": [4, 5],
    "d": [11]
    }

    result =
    for a_val in my_dict['a']:
    for b_val in my_dict['b']:
    for c_val in my_dict['c']:
    for d_val in my_dict['d']:
    result.append({'a': a_val, 'b': b_val, 'c': c_val, 'd': d_val})

    print(result)


    This gives the expected result.






    share|improve this answer
























    • Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

      – Furlings
      Nov 22 '18 at 9:19











    • No problem. Then the itertools approach is definitely the right one!

      – DatHydroGuy
      Nov 22 '18 at 9:20
















    0














    Assuming that you are only interested in my_dict having 4 keys, it is simple enough to use nested for loops:



    my_dict = {
    "a": [1, 2, 3],
    "b": [10],
    "c": [4, 5],
    "d": [11]
    }

    result =
    for a_val in my_dict['a']:
    for b_val in my_dict['b']:
    for c_val in my_dict['c']:
    for d_val in my_dict['d']:
    result.append({'a': a_val, 'b': b_val, 'c': c_val, 'd': d_val})

    print(result)


    This gives the expected result.






    share|improve this answer
























    • Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

      – Furlings
      Nov 22 '18 at 9:19











    • No problem. Then the itertools approach is definitely the right one!

      – DatHydroGuy
      Nov 22 '18 at 9:20














    0












    0








    0







    Assuming that you are only interested in my_dict having 4 keys, it is simple enough to use nested for loops:



    my_dict = {
    "a": [1, 2, 3],
    "b": [10],
    "c": [4, 5],
    "d": [11]
    }

    result =
    for a_val in my_dict['a']:
    for b_val in my_dict['b']:
    for c_val in my_dict['c']:
    for d_val in my_dict['d']:
    result.append({'a': a_val, 'b': b_val, 'c': c_val, 'd': d_val})

    print(result)


    This gives the expected result.






    share|improve this answer













    Assuming that you are only interested in my_dict having 4 keys, it is simple enough to use nested for loops:



    my_dict = {
    "a": [1, 2, 3],
    "b": [10],
    "c": [4, 5],
    "d": [11]
    }

    result =
    for a_val in my_dict['a']:
    for b_val in my_dict['b']:
    for c_val in my_dict['c']:
    for d_val in my_dict['d']:
    result.append({'a': a_val, 'b': b_val, 'c': c_val, 'd': d_val})

    print(result)


    This gives the expected result.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 9:13









    DatHydroGuyDatHydroGuy

    6812413




    6812413













    • Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

      – Furlings
      Nov 22 '18 at 9:19











    • No problem. Then the itertools approach is definitely the right one!

      – DatHydroGuy
      Nov 22 '18 at 9:20



















    • Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

      – Furlings
      Nov 22 '18 at 9:19











    • No problem. Then the itertools approach is definitely the right one!

      – DatHydroGuy
      Nov 22 '18 at 9:20

















    Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

    – Furlings
    Nov 22 '18 at 9:19





    Thank you for answer but I can't use it because "my_dict" can contain different number of key with different name.

    – Furlings
    Nov 22 '18 at 9:19













    No problem. Then the itertools approach is definitely the right one!

    – DatHydroGuy
    Nov 22 '18 at 9:20





    No problem. Then the itertools approach is definitely the right one!

    – DatHydroGuy
    Nov 22 '18 at 9:20











    0














    You can use:



    from itertools import product
    allNames = sorted(my_dict)
    values= list(product(*(my_dict[Name] for Name in allNames)))
    d = list(dict(zip(['a','b','c','d'],i)) for i in values)


    Output:



    [{'a': 1, 'c': 4, 'b': 10, 'd': 11}, 
    {'a': 1, 'c': 5, 'b': 10, 'd': 11},
    {'a': 2, 'c': 4, 'b': 10, 'd': 11},
    {'a': 2, 'c': 5, 'b': 10, 'd': 11},
    {'a': 3, 'c': 4, 'b': 10, 'd': 11},
    {'a': 3, 'c': 5, 'b': 10, 'd': 11}]





    share|improve this answer




























      0














      You can use:



      from itertools import product
      allNames = sorted(my_dict)
      values= list(product(*(my_dict[Name] for Name in allNames)))
      d = list(dict(zip(['a','b','c','d'],i)) for i in values)


      Output:



      [{'a': 1, 'c': 4, 'b': 10, 'd': 11}, 
      {'a': 1, 'c': 5, 'b': 10, 'd': 11},
      {'a': 2, 'c': 4, 'b': 10, 'd': 11},
      {'a': 2, 'c': 5, 'b': 10, 'd': 11},
      {'a': 3, 'c': 4, 'b': 10, 'd': 11},
      {'a': 3, 'c': 5, 'b': 10, 'd': 11}]





      share|improve this answer


























        0












        0








        0







        You can use:



        from itertools import product
        allNames = sorted(my_dict)
        values= list(product(*(my_dict[Name] for Name in allNames)))
        d = list(dict(zip(['a','b','c','d'],i)) for i in values)


        Output:



        [{'a': 1, 'c': 4, 'b': 10, 'd': 11}, 
        {'a': 1, 'c': 5, 'b': 10, 'd': 11},
        {'a': 2, 'c': 4, 'b': 10, 'd': 11},
        {'a': 2, 'c': 5, 'b': 10, 'd': 11},
        {'a': 3, 'c': 4, 'b': 10, 'd': 11},
        {'a': 3, 'c': 5, 'b': 10, 'd': 11}]





        share|improve this answer













        You can use:



        from itertools import product
        allNames = sorted(my_dict)
        values= list(product(*(my_dict[Name] for Name in allNames)))
        d = list(dict(zip(['a','b','c','d'],i)) for i in values)


        Output:



        [{'a': 1, 'c': 4, 'b': 10, 'd': 11}, 
        {'a': 1, 'c': 5, 'b': 10, 'd': 11},
        {'a': 2, 'c': 4, 'b': 10, 'd': 11},
        {'a': 2, 'c': 5, 'b': 10, 'd': 11},
        {'a': 3, 'c': 4, 'b': 10, 'd': 11},
        {'a': 3, 'c': 5, 'b': 10, 'd': 11}]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:24









        JoeJoe

        6,05421429




        6,05421429























            0














            itertools.product produces the combinations of a list of iterators.



            dict.values() gets the list needed.



            For each combination, zip up the dict.keys() with the combination.



            Use a list comprehension to collect them up:



            from itertools import product
            from pprint import pprint

            my_dict = {
            "a":[1, 2, 3],
            "b":[10],
            "c":[4, 5],
            "d":[11]
            }

            result = [dict(zip(my_dict,i)) for i in product(*my_dict.values())]
            pprint(result)


            Output:



            [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
            {'a': 1, 'b': 10, 'c': 5, 'd': 11},
            {'a': 2, 'b': 10, 'c': 4, 'd': 11},
            {'a': 2, 'b': 10, 'c': 5, 'd': 11},
            {'a': 3, 'b': 10, 'c': 4, 'd': 11},
            {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





            share|improve this answer
























            • Thank you very much for explanation ! It's more clear now ;)

              – Furlings
              Nov 22 '18 at 9:34
















            0














            itertools.product produces the combinations of a list of iterators.



            dict.values() gets the list needed.



            For each combination, zip up the dict.keys() with the combination.



            Use a list comprehension to collect them up:



            from itertools import product
            from pprint import pprint

            my_dict = {
            "a":[1, 2, 3],
            "b":[10],
            "c":[4, 5],
            "d":[11]
            }

            result = [dict(zip(my_dict,i)) for i in product(*my_dict.values())]
            pprint(result)


            Output:



            [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
            {'a': 1, 'b': 10, 'c': 5, 'd': 11},
            {'a': 2, 'b': 10, 'c': 4, 'd': 11},
            {'a': 2, 'b': 10, 'c': 5, 'd': 11},
            {'a': 3, 'b': 10, 'c': 4, 'd': 11},
            {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





            share|improve this answer
























            • Thank you very much for explanation ! It's more clear now ;)

              – Furlings
              Nov 22 '18 at 9:34














            0












            0








            0







            itertools.product produces the combinations of a list of iterators.



            dict.values() gets the list needed.



            For each combination, zip up the dict.keys() with the combination.



            Use a list comprehension to collect them up:



            from itertools import product
            from pprint import pprint

            my_dict = {
            "a":[1, 2, 3],
            "b":[10],
            "c":[4, 5],
            "d":[11]
            }

            result = [dict(zip(my_dict,i)) for i in product(*my_dict.values())]
            pprint(result)


            Output:



            [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
            {'a': 1, 'b': 10, 'c': 5, 'd': 11},
            {'a': 2, 'b': 10, 'c': 4, 'd': 11},
            {'a': 2, 'b': 10, 'c': 5, 'd': 11},
            {'a': 3, 'b': 10, 'c': 4, 'd': 11},
            {'a': 3, 'b': 10, 'c': 5, 'd': 11}]





            share|improve this answer













            itertools.product produces the combinations of a list of iterators.



            dict.values() gets the list needed.



            For each combination, zip up the dict.keys() with the combination.



            Use a list comprehension to collect them up:



            from itertools import product
            from pprint import pprint

            my_dict = {
            "a":[1, 2, 3],
            "b":[10],
            "c":[4, 5],
            "d":[11]
            }

            result = [dict(zip(my_dict,i)) for i in product(*my_dict.values())]
            pprint(result)


            Output:



            [{'a': 1, 'b': 10, 'c': 4, 'd': 11},
            {'a': 1, 'b': 10, 'c': 5, 'd': 11},
            {'a': 2, 'b': 10, 'c': 4, 'd': 11},
            {'a': 2, 'b': 10, 'c': 5, 'd': 11},
            {'a': 3, 'b': 10, 'c': 4, 'd': 11},
            {'a': 3, 'b': 10, 'c': 5, 'd': 11}]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 9:30









            Mark TolonenMark Tolonen

            94.4k12114176




            94.4k12114176













            • Thank you very much for explanation ! It's more clear now ;)

              – Furlings
              Nov 22 '18 at 9:34



















            • Thank you very much for explanation ! It's more clear now ;)

              – Furlings
              Nov 22 '18 at 9:34

















            Thank you very much for explanation ! It's more clear now ;)

            – Furlings
            Nov 22 '18 at 9:34





            Thank you very much for explanation ! It's more clear now ;)

            – Furlings
            Nov 22 '18 at 9:34


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427249%2fconvert-dict-to-list-of-dict-for-each-combinations%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith