[Tutor] Very basic question about lists

wesley chun wescpy at gmail.com
Mon Dec 22 21:19:40 CET 2008


>> if 'arr' or 'bell' in item:
>
> The interpreter sees this as
>  if ('arr') or ('bell' in item):
>
> 'arr' always evaluates to True so the condition is always true. The
> correct way to express this condition is
>  if 'arr' in item or 'bell' in item:

arrgh. yes, i saw this too but forgot to mention it in my reply. the
reason why it is, as kent has stated is that any non-empty container,
i.e., 'arr', always evaluates to Boolean True. the only string that
has a False value is the empty string, or "".

in addition, i think (and i may be wrong about this) that he really
wanted to do:

if 'arr' in list1 or 'bell' in list1...

"in item" is a substring check and not a list membership query.

-- wesley


More information about the Tutor mailing list