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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:47:45+00:00 2026-06-07T20:47:45+00:00

I have a Python dictionary as below: d={ ‘cs101’: {‘name’: ‘Building a Search Engine’,

  • 0

I have a Python dictionary as below:

d={ 'cs101': {'name': 'Building a Search Engine', 'teacher': 'Dave',
    'assistant': 'Peter C.'},
    'cs373': {'name': 'Programming a Robotic Car', 'teacher': 'Sebastian',
    'assistant': 'Andy'}
}

I need to find if the name ‘Peter’ is a teacher or assistant for any of these elements. Note that ‘Peter’ and ‘Peter C.’ are two different persons. Suppose coursename is a variable to loop through 'cs101' and 'cs373'. If I use ‘in’ keyword to check membership:

'Peter' in d[coursename]['assistant'] 

it will return True.

Similarly, '' in d[coursename]['assistant'] will also return True.

So, it seems that I cannot use the in keyword..

Can someone tell me how to check the membership?

  • 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-07T20:47:47+00:00Added an answer on June 7, 2026 at 8:47 pm

    Peter, where art thou?

    'Peter' in 'Peter Pan' will check if the string ‘Peter’ can be found somewhere in ‘Peter Pan’, which is obviously true.

    If you want an exact match you should use ==, as in the below example:

    'Peter' == d[coursename]['assistant']
    

    The above will do a case-sensitive match, if you’d like to do it case-insensitive you could write: 'Peter'.lower() == d[coursename]['assistant'].lower().


    Some examples of the differences between in and ==

    Python 2.7.3 (default, Apr 13 2012, 00:05:08) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    
    >>> 'Peter' in 'Peter Pan'
    True
    
    >>> 'Peter' in 'Prince Pan'
    False
    
    >>> 'Peter' == 'Peter'
    True
    
    >>> 'Peter' == 'Prince'
    False
    
    >>> '' in 'Bananas'
    True
    
    >>> '' == 'Bananas'
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python dictionary that labels key name to attributes. The program that
I have a python dictionary e.g.: [{pk:1,name:John,size:1/4 },{},{},etc] That size is 1/4 inch,how would
I have a Python dictionary like mydict = {'name':'abc','city':'xyz','country','def'} . How should I check
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I have data in a python dictionary that I'd like to store in a
I have a python function that randomize a dictionary representing a position specific scoring
I have a process that pickles a dictionary using Python 3.2. I then need
I'm newish to Python (and stackoverflow)...bear with me! I have a dictionary that looks
In Python, you can have key,value pairs in a dictionary where you can loop
I have a python dictionary. Just to give out context, I am trying to

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.