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

  • Home
  • SEARCH
  • 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 8831307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:16:50+00:00 2026-06-14T08:16:50+00:00

I want to generate all possible ways of using dicts, based on the values

  • 0

I want to generate all possible ways of using dicts, based on the values in them. To explain in code, I have:

a = {'name' : 'a', 'items': 3}
b = {'name' : 'b', 'items': 4}
c = {'name' : 'c', 'items': 5}

I want to be able to pick (say) exactly 7 items from these dicts, and all the possible ways I could do it in.

So:

x = itertools.product(range(a['items']), range(b['items']), range(c['items']))
y = itertools.ifilter(lambda i: sum(i)==7, x)

would give me:

(0, 3, 4)
(1, 2, 4)
(1, 3, 3)
...

What I’d really like is:

({'name' : 'a', 'picked': 0}, {'name': 'b', 'picked': 3}, {'name': 'c', 'picked': 4})
({'name' : 'a', 'picked': 1}, {'name': 'b', 'picked': 2}, {'name': 'c', 'picked': 4})
({'name' : 'a', 'picked': 1}, {'name': 'b', 'picked': 3}, {'name': 'c', 'picked': 3})
....

Any ideas on how to do this, cleanly?

  • 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-14T08:16:52+00:00Added an answer on June 14, 2026 at 8:16 am

    Here it is

    import itertools
    import operator
    
    a = {'name' : 'a', 'items': 3}
    b = {'name' : 'b', 'items': 4}
    c = {'name' : 'c', 'items': 5}
    
    dcts = [a,b,c]
    
    x = itertools.product(range(a['items']), range(b['items']), range(c['items']))
    y = itertools.ifilter(lambda i: sum(i)==7, x)
    z = (tuple([[dct, operator.setitem(dct, 'picked', vval)][0] \
           for dct,vval in zip(dcts, val)]) for val in y)
    for zz in z:
        print zz
    

    You can modify it to create copies of dictionaries. If you need a new dict instance on every iteration, you can change z line to

    z = (tuple([[dct, operator.setitem(dct, 'picked', vval)][0] \
          for dct,vval in zip(map(dict,dcts), val)]) for val in y)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c++ bitset of given length. I want to generate all possible
Possible Duplicate: How to generate all permutations of a list in Python I want
I want to generate a heat map from an all-vs-all comparison. I have the
Let's say I have a class of 30 students and want generate every possible
I want to generate all possible permutations for specific consonant/vowel setups. For example, I
Given a positive integer n, I want to generate all possible n bit combinations
I want to generate a list of all possible combinations of a list of
this is my simple code to generate all possible combinations of a set for
I want to generate all possible sequence for 49 6 lottery So I need
Given List {1,2,3,4,5} I want to generate all possible sequences of those numbers in

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.