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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:34:27+00:00 2026-05-26T03:34:27+00:00

I have some Python dictionaries like this: A = {id: {idnumber: condition},…. e.g. A

  • 0

I have some Python dictionaries like this:

A = {id: {idnumber: condition},.... 

e.g.

A = {1: {11 : 567.54}, 2: {14 : 123.13}, .....

I need to search if the dictionary has any idnumber == 11 and calculate something with the condition. But if in the entire dictionary doesn’t have any idnumber == 11, I need to continue with the next dictionary.

This is my try:

for id, idnumber in A.iteritems():
    if 11 in idnumber.keys(): 
       calculate = ......
    else:
       break
  • 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-26T03:34:28+00:00Added an answer on May 26, 2026 at 3:34 am

    You’re close.

    idnum = 11
    # The loop and 'if' are good
    # You just had the 'break' in the wrong place
    for id, idnumber in A.iteritems():
        if idnum in idnumber.keys(): # you can skip '.keys()', it's the default
           calculate = some_function_of(idnumber[idnum])
           break # if we find it we're done looking - leave the loop
        # otherwise we continue to the next dictionary
    else:
        # this is the for loop's 'else' clause
        # if we don't find it at all, we end up here
        # because we never broke out of the loop
        calculate = your_default_value
        # or whatever you want to do if you don't find it
    

    If you need to know how many 11s there are as keys in the inner dicts, you can:

    idnum = 11
    print sum(idnum in idnumber for idnumber in A.itervalues())
    

    This works because a key can only be in each dict once so you just have to test if the key exits. in returns True or False which are equal to 1 and 0, so the sum is the number of occurences of idnum.

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

Sidebar

Related Questions

I have some Python code where I need to call upon a dictionary entry
I have some Python code that looks something like this: rates = {3: [((17500,
I have some really nice Python code to do what I need to do.
I have some python module, which has a class ModuleClass and I can't modify
I have some Python Scripts which I would like to use from my VB.NET
I have some python code that does a certain task. I need to call
Suppose I have some Python code like the following: input = open(input.txt) x =
I have some Python code that works correctly when I use python.exe to run
I have some old python code that uses the pywin32 extensions. Starting out with
I have created some python code which creates an object in a loop, and

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.