[Tutor] Faster list searching?

wesley chun wescpy at gmail.com
Wed Nov 18 23:50:37 CET 2009


On Wed, Nov 18, 2009 at 1:51 PM, GoodPotatoes <goodpotatoes at yahoo.com> wrote:
> I'm dealing with bigger lists than I have been, and noticed this is getting
> really slow.  Is there a faster way to do this?
>
> for x in list1:
>     if x not in list2:
>         list3.append(x)
>
> My search is taking up to 5 minutes to complete.


greetings! hopefully this isn't a homework problem as we cannot help
with those. can you give us an example of the lists that you're using
(not the entire things, but just tell us what they contain)? also, use
the timeit module to show us some numbers to confirm what you're
seeing.

my suggestion would be to use sets if possible instead of lists since
those lookups are way faster (hash vs. sequence). if you're using
Python 3, i think you can even build the solution set using set
comprehensions.

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list