List question

Ricky Zhou ricky.zhou at gmail.com
Fri Mar 21 17:25:36 EDT 2008


On 2008-03-21 05:16:41 PM, From at home.xs4all.nl wrote:
> alist = [] 
> blist = [ 'one','two','one and two','one and four','five','one two']
> for f in blist:
> 	if 'one' and 'two' in f:
> 		alist.append(f)
>    
> for i in alist:
>     print i
> 
> two
> one and two
> one two
> 
> 
> why is it printing the first "two"?

Look at the this line:

    if 'one' and 'two' in f:

You're basically saying:

    if ('one') and ('two' in f):

which is why you only get elements that contain 'two'.  

Ricky
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080321/254fc224/attachment-0001.sig>


More information about the Python-list mailing list