'Advanced' list comprehension? query

scottishguy scottishguy at gmail.com
Wed Aug 8 17:29:53 EDT 2007


Thanks for some great explanations and alternatives!

I'll go with the any() approach, as its nice and readable (my
noShowList is quite small compared to myList).  Thanks, John, for the
'make it obvious' tip.

As an exercise, if I now wanted to show the entries where the
'noShowList' matches, what would be the best option (using list
comprehension)

i.e. ['item one', 'item three']

My original version:
print [x for x in myList for y in noShowList if y in x]

Version based on previous answer:
print [x for x in myList if any(y in x for y in noShowList)]

Cheers!




More information about the Python-list mailing list