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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:25:46+00:00 2026-06-17T15:25:46+00:00

I have a really weird thing happening. I am trying to loop through a

  • 0

I have a really weird thing happening. I am trying to loop through a set of dictionaries to find all of the items with a specific value related to a field key. Take the following:

ex_set is an output from a mysql system that I do not have control over. If I had to re-create it using python, it would be something like:

dict_a [ 'field' ] = 'fruit'
dict_a [ 'value' ] = 'apple'
dict_b [ 'field' ] = 'fruit'
dict_b [ 'value' ] = 'berry'
ex_set = set()
ex_set.add (dict_a,dict_b)

The important thing is how the set looks when I pprint it.

pprint (ex_set)
OUTPUTS> ({'field' : 'fruit',
        'value' : 'apple'},
        'field' : 'fruit'},
        'value' : 'berry'})

i = 0
while len ( ex_set ) > i:
    for k , v in ex_set [i].iteritems ( ):
        if v == 'fruit':
        pprint ( ex_set[i] )
    i += 1

The problem is that the printing of this does not print all of the dictionaries that have a value = “fruit”.

Is there a better way to search through a set of dictionaries? The set that I am searching through has 3 key/value combinations in each dictionary and about 30k dictionaries. This works about 25% of the time and I can’t figure out why it is only returning about 20% of the matches.

Thanks for the help!

  • 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-17T15:25:47+00:00Added an answer on June 17, 2026 at 3:25 pm

    Based on your description you are seeking something like:

    In [6]: ex_set = ({'fruit':'apple',
       ...:            'value':'fruit'},
       ...:           {'fruit':'fruit'},
       ...:           {'apple':'apple'})
    
    In [7]: for d in ex_set:
       ...:     if 'fruit' in d.values():
       ...:         print(d)
       ...:         
    {'fruit': 'apple', 'value': 'fruit'}
    {'fruit': 'fruit'}
    

    Also, in addition to the fact, that your example isn’t valid python, ex_set certainly can’t be a set, as sets's cant contain dictionaries as they are unhashable. I would consider renaming it to something more suitable:

    In [8]: set([{}])
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-8-7facc835553f> in <module>()
    ----> 1 set([{}])
    
    TypeError: unhashable type: 'dict'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really weird thing happening in my wpf application. Every time I
I have a really weird thing happening. In my app delegate, I'm calling presentModalViewController:
IDE = VS7 or 2002 Hi all, I have a really weird problem here.
I would be grateful to find out some really weird things I have seen
There is an really weird thing happening with my listview. I am creating an
it's a really weird thing - i have a website that works perfectly in
I have really weird problem with colors in interface builder. I set a color
I have a really weird issue here. I'm using my local development server right
I have a really weird problem with fgets() in C. Below is the code
I've been having a really weird problem. I have a mutable array that is

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.