list comprehension problem

Falcolas garrickp at gmail.com
Thu Oct 29 17:08:15 CET 2009


On Oct 29, 9:31 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> mk wrote:
> > Hello everyone,
>
> > print hosts
> > hosts = [ s.strip() for s in hosts if s is not '' and s is not None and
> > s is not '\n' ]
> > print hosts
>
> > ['9.156.44.227\n', '9.156.46.34 \n', '\n']
> > ['9.156.44.227', '9.156.46.34', '']
>
> > Why does the hosts list after list comprehension still contain '' in
> > last position?
>
> > I checked that:
>
> > print hosts
> > hosts = [ s.strip() for s in hosts if s != '' and s != None and s != '\n'
> > ] print hosts
>
> > ..works as expected:
>
> > ['9.156.44.227\n', '9.156.46.34 \n', '\n']
> > ['9.156.44.227', '9.156.46.34']
>
> > Are there two '\n' strings in the interpreter's memory or smth so the
> > identity check "s is not '\n'" does not work as expected?
>
> > This is weird. I expected that at all times there is only one '\n'
> > string in Python's cache or whatever that all labels meant by the
> > programmer as '\n' string actually point to. Is that wrong assumption?
>
> Yes. Never use "is" unless you know 100% that you are talking about the same
> object, not just equality.
>
> Diez

I'd also recommend trying the following filter, since it is identical
to what you're trying to do, and will probably catch some additional
edge cases without any additional effort from you.

[s.strip() for s in hosts if s.strip()]

This will check the results of s.strip(), and since empty strings are
considered false, they will not make it into your results.

Garrick



More information about the Python-list mailing list