query on python list
Tim Hochberg
tim.hochberg at ieee.org
Tue Dec 27 22:33:39 EST 2005
muttu2244 at yahoo.com wrote:
> hi all
> am searching for a key in a list, am using
>
> Found = 0
> for item in list:
> if not key == item:
> Found = 0
> elif key == item:
> Found =1
>
> Now based on the Found value i ll manipulate the list.
> but whenever the "key and item" doesnt match it makes "Found = 0", even
> though i have an item that matches the key, but it overwrites the Found
> variable to 0, in the next loop when "key ! = item", and hence all my
> calculations are going wrong,
> If i find a key in my list, some how the variable "Found " should be
> 1,till it comes out of the loop.
>
> How can i do that.
Well, one way would be:
Found = False
for item in list:
if key == item:
Found = True
break
or maybe:
for item in list:
if key == item:
Found = True
break
else:
Found = False
but a better way would be:
Found = (key in list)
-tim
>
> thanks in advance
> yogi
>
More information about the Python-list
mailing list