fast list search?

Javier Zaragozá Arenas fjzaragoza at ono.com
Fri Jun 11 19:54:35 EDT 2004


I think the better way is sort before the list

>> thelist.sort()

And then, a binary search

>> point_of_insertion = bisect.bisect(thelist, item)
>> is_present = thelist[point_of_insertion -1:point_of_insertion] == [item]
 >> if not is_present:
....            thelist.insert(point_of_insertion, item)

and done!

Fco Javier Zaragozá Arenas



"ramon aragues" <ramon.aragues at gmx.net> escribió en el mensaje
news:mailman.747.1086774432.6949.python-list at python.org...
> 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?
>
> Thanks a lot,
>
> Ramon Aragues
>
>





More information about the Python-list mailing list