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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:16:10+00:00 2026-06-10T03:16:10+00:00

>>> x = { ‘a’ : ‘b’ , ‘c’ : ‘d’ } >>>’a’ and

  • 0
>>> x = { 'a' : 'b' , 'c' : 'd' }

>>>'a' and 'c' in x
True

>>>'a' and 'b' in x
False

>>>'b' and 'c' in x
True

If in <dict> checks for keys, how come the last one that looks up b returns true even if there is no such key b?

  • 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-10T03:16:12+00:00Added an answer on June 10, 2026 at 3:16 am

    You want 'b' in x and 'c' in x

    You’re misunderstanding how the and operator works (and you’ve got your operator precedence wrong). in has a higher precedence than and, so your expression is parsed as:

    if 'b' and ('c' in x):
    

    which is the same as:

    if 'c' in x:
    

    because bool('b') is always True since 'b' is a non-empty string.

    Here is a table of operator precedence in python

    Note that even if and had higher precedence than in, you still wouldn’t be getting what you want because ('b' and 'c') in x would reduce down to 'c' in x since 'b' and 'c' returns 'c'.

    One way to rewrite your expression would be:

    if all( key in yourdict for key in ('b', 'c') ):
    

    This is overkill for just 2 keys to check, but quickly becomes useful if you have more keys to check.

    As a final comment, you’re probably trying to apply operator chaining (which is really neat). However, some operators don’t lend themselves to chaining very well (in is one of them). Expressions like 3 > 10 > 100 > 1000 do work by some strange python black magic. In my experience, relational operators chain nicely (‘<‘,’>’,’==’,'<=’,’>=’) but most of the other operators don’t chain in a way that is intuitive. In general,

    a operator b operator c operator ...
    

    is equivalent to:

    (a operator b) and (b operator c) and (c operator ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to create a RegEx expression that will successfully parse the following
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I was writing code for dragging mechanism which invokes to wait for small period

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.