On 01/28/2013 05:33 AM, Wolfgang Maier wrote:
Consider this:
some_names=["Adam", "Andrew", "Arthur", "Bob", "Caroline","Lancelot"] # a sorted list of names [n for n in some_names if n.startswith("A")] # certainly gives a list of all names starting with A, but . [n for n in some_names while n.startswith("A")] # would have saved two comparisons
What happens when you want the names that start with 'B'? The advantage of 'if' is it processes the entire list so grabs all items that match, and the list does not have to be ordered. The disadvantage (can be) that it processes the entire list. Given that 'while' would only work on sorted lists, and could only start from the beginning, I think it may be too specialized. But I wouldn't groan if someone wanted to code it up. :) +0 ~Ethan~