[Tutor] Using string.strip()

Wesley Chun wesc@deirdre.org
Wed, 8 Aug 2001 23:46:15 -0700 (PDT)


On Wed, 8 Aug 2001, Sheila King wrote:

> On Wed, 8 Aug 2001 09:15:28 +0200, Hans Nowak <hnowak@cuci.nl>  wrote
> about RE: [Tutor] Using string.strip():
>
> :Note that this for loop "as is" doesn't do anything to the responses list...
> :it just strips an item, then discards it. Maybe this is the reason why it
> :doesn't have the desired effect?
>
> Thanks to all who responded and pointed this out.
>
> As I said, I was having a dumb moment. I certainly should have known
> better.
>
> I especially liked the solution:
>
> responses = [item.strip() for item in responses]
>
> That is really the coolest. (I'm still not good at the list
> comprehensions thing.)


the example you brought up is one motivating reason for creating
list comprehensions... the syntax still seems a little weird to
me since it's relatively new.

as alan points out, it replaces the earlier syntax of (and in
many ways obsoleting map()):

responses = map(string.strip, responses)

another reason why is that it (list comprehensions) saves one
extra function call (to map()) and does not have to lookup
objects over and over.

anyway, just my $0.02.

cheers!

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Silicon Valley-SF Bay Area Python users group:  http://baypiggies.org

"Core Python Programming", Prentice Hall PTR, December 2000
    http://starship.python.net/crew/wesc/cpp/

wesley.j.chun :: wesc@baypiggies.org
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/