Canonical way of deleting elements from lists

2008-01-09 Thread Robert Latest
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

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Fredrik Lundh
Robert Latest wrote: 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

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Fredrik Lundh
Fredrik Lundh wrote: creating a new list is always almost the right way to do things like message = message.replace(always almost, almost always) -- http://mail.python.org/mailman/listinfo/python-list

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Hrvoje Niksic
Robert Latest [EMAIL PROTECTED] writes: From a list of strings I want to delete all empty ones. This works: while '' in keywords: keywords.remove('') If you're looking for a quick (no quadratic behavior) and convenient way to do it, you can do it like this: keywords = [s for s in

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Hrvoje Niksic
Hrvoje Niksic [EMAIL PROTECTED] writes: If you're looking for a quick (no quadratic behavior) and convenient way to do it, you can do it like this: keywords = [s for s in keywords if s != ''] It now occurred to me that a good compromise between convenience and efficiency that retains the

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Robert Latest
Fredrik Lundh wrote: keywords = filter(None, keywords) # get true items only Makes seinse. BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the manual, or is this

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Robert Latest
Hrvoje Niksic wrote: keywords[:] = (s for s in keywords if s) Looks good but is so far beyond my own comprehension that I don't dare include it in my code ;-) robert -- http://mail.python.org/mailman/listinfo/python-list

Re: Canonical way of deleting elements from lists

2008-01-09 Thread bearophileHUGS
Robert Latest: Fredrik Lundh wrote: keywords = filter(None, keywords) # get true items only Makes seinse. BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Nick Craig-Wood
Robert Latest [EMAIL PROTECTED] wrote: Hrvoje Niksic wrote: keywords[:] = (s for s in keywords if s) Looks good but is so far beyond my own comprehension that I don't dare include it in my code ;-) :-) Worth understanding thought I think - here are some hints keywords[:] = (s for

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Fredrik Lundh
Nick Craig-Wood wrote: Using keywords[:] stops the creation of another temporary list. in CPython, list[:] = iter actually creates a temporary list object on the inside, in case iter isn't already a list or a tuple. (see the implementation of PySequence_Fast() for details). /F --

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Sion Arrowsmith
Robert Latest [EMAIL PROTECTED] wrote: BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the manual, or is this an omission? 3.6 talks about features common to all

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Nick Craig-Wood
Fredrik Lundh [EMAIL PROTECTED] wrote: Nick Craig-Wood wrote: Using keywords[:] stops the creation of another temporary list. in CPython, list[:] = iter actually creates a temporary list object on the inside, in case iter isn't already a list or a tuple. (see the implementation of

Re: Canonical way of deleting elements from lists

2008-01-09 Thread Robert Latest
Sion Arrowsmith wrote: Robert Latest [EMAIL PROTECTED] wrote: BTW, where can I find all methods of the built-in types? Section 3.6 only talks about strings and mentions the list append() method only in an example. Am I too stupid to read the manual, or is this an omission? 3.6 talks about