compare list

Shi Mu samrobertsmith at
Tue Nov 15 08:30:38 CET 2005

> Hey Ben,
> first, as expected, the other two answers you received are better. :-)
> Sets are much better optimized for things like membership testing than
> are lists. I'm not competent to explain why; indeed, I keep
> overlooking them myself :-(
> Unfortunately, the indents got screwed up along the way. But the part
> of my code you asked about was:
>     for item in list1:
>         if item in list2:
>             if item + 1 in list1 and item + 1 in list2:
>                 return True
> In some detail:
> Consider each item in list1 in turn. If the item isn't in list2, move
> on, as there is no chance of it meeting your test. If the item is in
> list2, then it is pointful to check if your test is met. Given that
> you wanted consecutive numbers, the 'if item + 1 in list1 and item + 1
> in list2' condition checks if both lists (which contain item if we've
> gone this far) also contain item + 1. If they do, you wanted the
> function to return 1 (I changed it to True as more idiomatic). After
> my for loop is a return False which we will only hit if the condition
> you wanted to test was never satisfied.
> Does that clarify it?
> Finally, your response to Alex would have been much more useful if
> you'd quoted the error rather than just asserting that you got an
> error :-)
> Best,
> Brian vdB
is it possible to modify the codes to compare the two lists with not
necessarily consecutive numbers?
For example,
compare A and b will get true because the two lists have 5 and 9
together though they are indifferent order.
Best Regards,

More information about the Python-list mailing list