fast list lookup

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Klaus Neuner

    fast list lookup

    Hello,

    what is the fastest way to determine whether list l (with
    len(l)>30000) contains a certain element?


    Klaus
  • Simon Brunning

    #2
    Re: fast list lookup

    On Wed, 26 Jan 2005 06:45:29 -0800 (PST), Klaus Neuner
    <klaus_neuner82 @yahoo.de> wrote:[color=blue]
    > what is the fastest way to determine whether list l (with
    > len(l)>30000) contains a certain element?[/color]

    If the list isn't sorted, I doubt you'll do better than

    if an_element in my_list:
    # do whatever

    If the list is sorted, have a look at the bisect module.

    --
    Cheers,
    Simon B,
    simon@brunningo nline.net,

    Comment

    • Alex Martelli

      #3
      Re: fast list lookup

      Klaus Neuner <klaus_neuner82 @yahoo.de> wrote:
      [color=blue]
      > what is the fastest way to determine whether list l (with
      > len(l)>30000) contains a certain element?[/color]

      "if thecertaineleme nt in l:"

      is the fastest way unless there are properties of l which you're not
      telling us about, or unless what you need is not just to determine
      whether l contains a certain element but rather something different
      (such as doing the same determination for several elements on an
      unchanging l).


      Alex

      Comment

      • Kent Johnson

        #4
        Re: fast list lookup

        Klaus Neuner wrote:[color=blue]
        > Hello,
        >
        > what is the fastest way to determine whether list l (with
        > len(l)>30000) contains a certain element?[/color]

        If you can use a set or dict instead of a list this test will be much faster.

        Kent

        Comment

        • Marco Aschwanden

          #5
          Re: fast list lookup

          >> what is the fastest way to determine whether list l (with[color=blue][color=green]
          >> len(l)>30000) contains a certain element?[/color][/color]

          Either a sorted list (in conjunction with the bisect-module) or a
          dictionary is your friend...

          Regards,
          Marco

          Comment

          Working...