I've been trying to use more list comprehensions recently. 

I was just
fleshing something out which brought on my post. 

On Fri, 29 Oct 2010
09:15:47 -0400, Henry Olders  wrote: When dealing with lists, list
comprehensions are shorter and easier to work with:
l=['red','green','orange','blue','red','white']  [x for x in l if x in
['red','blue']] 
 Henry      

  On 2010-10-28, at 10:21 , Dan Ross wrote:


I don't think this is Mac specific, but I wonder if someone could explain
why these two groups of code behave differently: 

[code] 

colors =
['red', 'green', 'blue', 'orange', 'fuscia', 'black',
'white']

list_of_matches = []
for x in colors:
 if x == 'red' or 'green'
or 'blue':
 list_of_matches.append(x)
print
list_of_matches

list_of_matches2 = []
for x in colors:
 if x == 'red':

list_of_matches2.append(x)
 elif x == 'green':
 list_of_matches2.append(x)

elif x == 'blue':
 list_of_matches2.append(x)
 else:
 pass
print
list_of_matches2 

[/code] 

list_of_matches contains every item in colors.


list_of_matches2 only contains the matches, as I would expect. 

I don't
get it...... 

Thanks, 

Dan      

 
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

Reply via email to