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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:12:36+00:00 2026-06-03T07:12:36+00:00

When using the ‘in’ operator to search for an item in a list e.g.

  • 0

When using the ‘in’ operator to search for an item in a list e.g.

if item in list:
  print item

What algorithm is used to search for this item. Is it a straight search of the list from beginning to end or does it use something like binary search?

  • 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-03T07:12:37+00:00Added an answer on June 3, 2026 at 7:12 am

    lists can’t be assumed to be in sorted order (or any order at all), so binary search won’t work. Nor can the keys be assumed to be hashable, so unlike a dict or set a hash-table lookup can’t be used to accelerate the search

    At a guess it’s a straight-through check of every element from first to last.

    I’ll try and dig up the relevant Python source code.

    —

    Edit: The Python list.__contains__() function, which implements the in operator, is defined in listobject.c:

       393 static int
       394 list_contains(PyListObject *a, PyObject *el)
       395 {
       396     Py_ssize_t i;
       397     int cmp;
       398 
       399     for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
       400         cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
       401                                            Py_EQ);
       402     return cmp;
       403 }
    

    It iterates over every element in the list, from the first element to the last element (or until it finds a match.) No shortcuts here.

    —

    Edit 2: The plot thickens. If Python detects that you’re testing for membership of an element in a constant list or set, like:

    if letter in ['a','e','i','o','u']:    # list version
    if letter in {'a','e','i','o','u'}:    # set version
    

    Edit 3 [@JohnMachin]:

    The constant list is optimised to a constant tuple in 2.5-2.7 and 3.1-3.3.
    The constant set is optimised to a (constant) frozenset in 3.3.

    See also @CoryCarson’s answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
Using Flex 3, I would like to take an image snapshot such as this:
Using NSDateComponents I know how to get the day component, but this gives me
Using mercurial, I've run into an odd problem where a line from one committer
using a binary search tree I need to add to a vector all int
Using php/html, I want to retrieve email addresses (plus other information) from MySQL and
(Using MVC 2) From inside my controller action, I need to display the url:
Using Java,I have to fetch multiple sets of values from an XML file to
using file_get_contents , I open an Internet URL and get the contents of this
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter

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.