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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:17:42+00:00 2026-06-08T03:17:42+00:00

I am iterating through a list and comparing each element to 2 dictionaries. The

  • 0

I am iterating through a list and comparing each element to 2 dictionaries. The list elements are keys in the dictionaries. Some elements are in the 2 dictionaries, some are in one, some are in none.

for i in range(0,len(mylist)-1):
    if mylist[i] == mydict[mylist[i]]:
        print 'in dict 1'
    elif mylist[i] == mydict2[mylist[i]]:
        print 'in dict 2'
    else: print 'not in dictionaries'

My problem is it isn’t getting past the first elif statement. If it doesn’t find the list element in the 2 dictionaries, it prints a key error. I can’t understand it because I have another loop in another part of the code that’s very similar to this and works perfectly. If a key isn’t in a dictionary I want the else statement printed. Not a key error

  • 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-08T03:17:44+00:00Added an answer on June 8, 2026 at 3:17 am

    Problem 1, as mgilson said, is that = is assignment, == is equality. However, even with your question edit, if you are looking to find out if a key is in a dictionary, you should be using the in operator, not the equals. In other words, the form if key in dict:. So:

    for key in mylist:
        if key in mydict1:
            print 'Key %s in dict 1' % key
        elif key in mydict2:
            print 'Key %s in dict 2' % key
        else:
            print 'Key %s not in dictionaries' % key
    

    You could abstract this further to handle an arbitrary set of dicts with a function, if that would prove helpful in the long run (though for a small number of dicts, like 2, you are probably better of with the above hardcoded checking):

    def print_keys_from_list_in_dicts(key_list, dict_list):
        indexed_dict_list = enumerate(dict_list)
        for key in key_list:
            found_in_list = []
            for index, dict in indexed_dict_list:
                if key in dict:
                    found_in_list.append(index)
            print 'Key %s found in dicts %s.' % (key, found_in_list)
    
    print_keys_from_list_in_dicts(mylist, [mydict1, mydict2])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm iterating through a list of returned elements in a jQuery .each() call
I'm iterating through a List<> to find a matching element. The problem is that
I am having an issue removing elements of a list while iterating through the
I am iterating through a list of certain objects (each object has two attributes
I am iterating through a list in Python. mylist = ['a', 'b', 'c', 'd',
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Say I have an std::list<int> lst and some std::list<int>::iterator it for iterating through the
I am using CoreService2010Client , I am iterating through a list of TCM ID
Can you remove an item from a List<> whilst iterating through it? Will this
I'm iterating through my JSON response. I've got HTML elements with id's as the

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.