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 7171655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:27:28+00:00 2026-05-28T15:27:28+00:00

I have a list of dictionaries. Each dictionary has certain boolean flags. So, the

  • 0

I have a list of dictionaries. Each dictionary has certain boolean flags. So, the list looks like –

listA = [{key1: somevalue1, key2:somevalue2, flag1:True, flag2:False, score:0},
         {key1: somevalue3, key2:somevalue4, flag1:False, flag2:True, score:0},
         ...
         {key1: somevalue(N-1), key2:somevalueN, flag1:True, flag2:False, score:0}] 

Let’s say I have a table, that assigns scores based on the combination of values of the flags. It’s like a binary truth table:

flag1 True True False False
flag2 True False True False
score 1 2 3 4

I now want to iterate through the list, and assign these scores to each dictionary in the list, based upon the combination they specify. Is there an elegant way to do it, instead of if else loops? I have a lot of flags, and the combinations would increase with every new flag that’s added – result – the code is just ugly.

  • 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-05-28T15:27:29+00:00Added an answer on May 28, 2026 at 3:27 pm

    You can use a dictionary for this:

    scores = {(True, True): 1, (True, False): 2, 
              (False, True): 3, (False, False): 4}
    

    Now you can find the score by simply looking up scores[flag1, flag2].

    To do this on the whole list, use

    for d in listA:
        d["score"] = scores[d["flag1"], d["flag2"]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of dictionaries. Each dictionary has a colour attribute. For example:
I have a list that contains many dictionaries. Each dictionary represents a change that
I have a list of dictionaries as shown below. Each dictionary holds the total
I have a List<Dictionary<String, String>> dictionaries . Each dictionary will contain the following keys
quick and very basic newbie question. If i have list of dictionaries looking like
I have popened a process which is producing a list of dictionaries, something like:
I have a list of dictionaries like in this example: listofdict = [{'name': 'Foo',
I have a list of dictionaries like so: listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}] I want a
I have about 20,000 NSDictionary s. Each dictionary contains three NSString s. The dictionaries
Here is my problem: I have two dictionaries with identical structures: Dictionary<string, List<Object>> Existing

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.