Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:16:58+00:00 2026-06-07T06:16:58+00:00

How can I transform a dictionary of strings to lists to a list of

  • 0

How can I transform a dictionary of strings to lists to a list of dictionaries that map strings to values in those lists? For example, the following dictionary

{'a': [1,2],
 'b': ['x', 'y', 'z']}

would be transformed into the following list

[{'a': 1, 'b': 'x'}, {'a': 1, 'b': 'y'}, {'a': 1, 'b': 'z'},
 {'a': 2, 'b': 'x'}, {'a': 2, 'b': 'y'}, {'a': 2, 'b': 'z'},]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:16:59+00:00Added an answer on June 7, 2026 at 6:16 am

    The main thing you want here is the product of the values, and then to recreate the dictionaries. We can actually do this easily with the help of itertools.product():

    >>> from itertools import product
    >>> test = {'a': [1, 2], 'b': ['x', 'y', 'z']}
    >>> [dict(zip(test.keys(), part)) for part in product(*test.values())]
    [{'a': 1, 'b': 'x'}, {'a': 1, 'b': 'y'}, {'a': 1, 'b': 'z'}, 
     {'a': 2, 'b': 'x'}, {'a': 2, 'b': 'y'}, {'a': 2, 'b': 'z'}]
    

    What we do is we make the product all of the lists in the dictionary, and then take each produced pair (or in the case of more items in the dictionary, arbitrarily long tuple) and zip it to the keys in the dictionary. We then pass this into the dict() builtin to make a dictionary. This is all inside a list comprehension to quickly make the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following List which contains the following collection. How i can transform
How would look the code that can transform an upper triangular matrix into a
I have been thinking about a regular expression that can transform a list like
Can somebody give a good explanation of FFT image transform How the FFT transformed
i wonder how i can transform exactly the following piece of code to scandir
Is there any javascript plugin/library/script that will can transform a standard PHP to use
Are there any tools that can transform C++ code to xml, or some other
I want to create an application that can transform text the user enters in
Is there any automatic tool that I can transform legacy uniprocessor programs to the
Is there any function (VB.NET or C#) that can transform an English word into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.