"Canonical" way of deleting elements from lists

Robert Latest boblatest at yahoo.com
Wed Jan 9 05:01:14 EST 2008


Hello,

>From a list of strings I want to delete all empty ones. This works:

    while '' in keywords: keywords.remove('')

However, to a long-term C programmer this looks like an awkward way of 
accomplishing a simple goal, because the list will have to be re-evaluated 
in each iteration. Is there a way to just walk the list once and throw out 
unwanted elements as one goes along?

I started programming back when such little things were real performance 
issues, so I have some sort of cringe reflex when something looks 
inefficient.

robert



More information about the Python-list mailing list