[Tutor] nested loops
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Aug 22 20:02:09 CEST 2005
On Mon, 22 Aug 2005, Kent Johnson wrote:
> > Is there any way more efficient for run a nested loop?
> >
> > ------
> > for a in list_a:
> > for b in list_b:
> > if a == b: break
Hi Jonas,
Depends on what we're trying to do. Is it necessary to have a nested loop
here? What kind of problem is this trying to solve?
If the question is: "are any elements in list_a shared in list_b?", then
yes, we can avoid nested loops altogether. If we're concerned about
efficiency, we can take advanatage of dictionaries, or use something like
the 'set' data structure.
http://www.python.org/doc/lib/module-sets.html
More information about the Tutor
mailing list