fast list search?

Terry Reedy tjreedy at udel.edu
Wed Jun 9 07:04:19 EDT 2004


"Thomas Guettler" <guettli at thomas-guettler.de> wrote in message
news:pan.2004.06.09.10.22.54.20397 at thomas-guettler.de...
> Am Wed, 09 Jun 2004 11:49:19 +0200 schrieb ramon aragues:
>
> > Hi,
> >
> > I´ve got a list with more than 500,000 ints. Before inserting new ints,
> > I have to check that it doesn´t exist already in the list.
> >
> > Currently, I am doing the standard:
> >
> > if new_int not in long_list:
> > long_list.append(new_int)
> >
> >
> > but it is extremely slow... is there a faster way of doing this in
python?
>
> Hi,
>
> Use a dictionary instead of the list:
>
> if not long_dict.has_key(new_int):
>     long_dict[new_int]=1

Or use a set, which I believe will be faster (more C-coded) in 2.4.  That
eliminates the dummy value.

TJR







More information about the Python-list mailing list