[Tutor] Comparing to list elements
Glen Wheeler
gew75 at hotmail.com
Tue Aug 17 07:28:18 CEST 2004
I'll present a solution in the form of a list comprehension.
That way, if you don't know about these, then it incites further education
:).
>>> aList = [ 'hi', 'hello', 'welcome' ]
>>> sString = 'goodbye'
>>> [x for x in aList if x == sString]
[]
>>> if [x for x in aList if x == sString]:
.. print "sString is in aList"
..
>>>
HTH,
Glen
----- Original Message -----
From: "Bernard Lebel" <python at bernardlebel.com>
To: <tutor at python.org>
Sent: Tuesday, August 17, 2004 7:50 AM
Subject: [Tutor] Comparing to list elements
> Hello,
>
> I wish to compare a variable value to the elements from a list.
> For example:
>
> aList = [ 'hi', 'hello', 'welcome' ]
> sString = 'goodbye'
>
>
> Now I want to check if 'goodbye' is present in aList, and if not, take
> action.
>
>
> Thanks
> Bernard
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list