[Tutor] Comparing to list elements
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"
----- 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
> 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
> Tutor maillist - Tutor at python.org
More information about the Tutor