Skip to content
Advertisement

Expand a dict containing list items into a list of dict pairs

If I have a dictionary containing lists in one or more of its values:

JavaScript

How can I get a list of dict tuples paired by pair and iterating over c, with all else remaining constant? E.g.

JavaScript

Advertisement

Answer

Well, this doesn’t feel especially elegant, but you might use a nested for loop or list comprehension:

JavaScript

or

JavaScript

A cleaner solution might separate out the generation of the component dict into a function, like this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement