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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:21:43+00:00 2026-06-12T03:21:43+00:00

I’m making a Python parser, and this is really confusing me: >>> 1 in

  • 0

I’m making a Python parser, and this is really confusing me:

>>> 1 in [] in 'a'
False

>>> (1 in []) in 'a'
TypeError: 'in <string>' requires string as left operand, not bool

>>> 1 in ([] in 'a')
TypeError: 'in <string>' requires string as left operand, not list

How exactly does in work in Python, with regards to associativity, etc.?

Why do no two of these expressions behave the same way?

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

    1 in [] in 'a' is evaluated as (1 in []) and ([] in 'a').¹

    Since the first condition (1 in []) is False, the whole condition is evaluated as False; ([] in 'a') is never actually evaluated, so no error is raised.

    We can see how Python executes each statement using the dis module:

    >>> from dis import dis
    >>> dis("1 in [] in 'a'")
      1           0 LOAD_CONST               0 (1)
                  2 BUILD_LIST               0
                  4 DUP_TOP
                  6 ROT_THREE
                  8 CONTAINS_OP              0        # `in` is the contains operator
                 10 JUMP_IF_FALSE_OR_POP    18        # skip to 18 if the first 
                                                      # comparison is false
                 12 LOAD_CONST               1 ('a')  # 12-16 are never executed
                 14 CONTAINS_OP              0        # so no error here (14)
                 16 RETURN_VALUE
            >>   18 ROT_TWO
                 20 POP_TOP
                 22 RETURN_VALUE
    >>> dis("(1 in []) in 'a'")
      1           0 LOAD_CONST               0 (1)
                  2 LOAD_CONST               1 (())
                  4 CONTAINS_OP              0        # perform 1 in []
                  6 LOAD_CONST               2 ('a')  # now load 'a'
                  8 CONTAINS_OP              0        # check if result of (1 in []) is in 'a'
                                                      # throws Error because (False in 'a')
                                                      # is a TypeError
                 10 RETURN_VALUE
    >>> dis("1 in ([] in 'a')")
      1           0 LOAD_CONST               0 (1)
                  2 BUILD_LIST               0
                  4 LOAD_CONST               1 ('a')
                  6 CONTAINS_OP              0        # perform ([] in 'a'), which is 
                                                      # incorrect, so it throws a TypeError
                  8 CONTAINS_OP              0        # if no Error then this would 
                                                      # check if 1 is in the result of ([] in 'a')
                 10 RETURN_VALUE
    

    1. Except that [] is only evaluated once. This doesn’t matter in this example but if you (for example) replaced [] with a function that returned a list, that function would only be called once (at most). The documentation explains also this.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.