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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:11:57+00:00 2026-06-09T19:11:57+00:00

For the built-in python containers ( list , tuple , etc) the in operator

  • 0

For the built-in python containers (list, tuple, etc) the in operator is equivalent to any(y == item for item in container) with the caveat that the former method is faster (and prettier):

In [13]: container = range(10000)
In [14]: %timeit (-1 in container)
1000 loops, best of 3: 241 us per loop
In [15]: %timeit any(-1 == item for item in container)
1000 loops, best of 3: 1.2 ms per loop

Is there an equivalent to any(y is item for item in container)? That is, a test that uses is instead of ==?

  • 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-09T19:11:59+00:00Added an answer on June 9, 2026 at 7:11 pm

    Nope, there isn’t. The is operator is just not needed that often to justify having to maintain a C-optimized method and adding confusion to the python API.

    The in test for lists and tuples does do a full search similar to any, albeit in C, btw. In sets however, the test makes use of the efficient storage algorithm underlying the container and the search takes constant time in the expected case. For both sets and mappings, keys are supposed to have a stable hash, which in most cases means is should not be needed, really.

    So, the correct spelling is:

    # For sequences
    any(y is item for item in container)
    
    # For sets, short circuit first for the not-present case:
    # (note that you normally should not need this as you are supposed to rely on the hash)
    y in setcontainer and any(y is item for item in setcontainer)
    
    # For mappings, y is a key
    y in mapping 
    
    # For mappings, y is a value, and you do not have a key, fall back to any
    any(y is item for item in mapping.itervalues())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary)
I would like to know if the Python built-in containers (list, vector, set...) are
I'm having an issue considering the built-in Python List-methods. As I learned Python, I
There are so many html and xml libraries built into python , that it's
I'm wondering if there's a reason that there's no first(iterable) in the Python built-in
In my code there's numerous comparisons for equality of various containers (list, dict, etc.).
Is there any build-in function in Python that merges two lists into a dict?
[Python 3.1] I am trying to create a hash for a container that may
I'm trying to build a class that inherits methods from Python's list, but also
All the Python built-ins are subclasses of object and I come across many user-defined

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.