[Tutor] Faster list searching?

Bill Campbell bill at celestial.net
Thu Nov 19 00:03:22 CET 2009


On Wed, Nov 18, 2009, GoodPotatoes 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.

When I have had to deal with large lists, I have found that using
an intermediate dictionary can save huge amounts of time.
Something like:

dict2 = {}.fromkeys(list2)
for x in list1:
	if x not in dist2:
		dict2[x] = True

list2 = dict2.keys()

Bill
-- 
INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Intaxication: Euphoria at getting a refund from the IRS, which lasts until
you realize it was your money to start with.


More information about the Tutor mailing list