Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78253

Best way to deal with different data types in a list comprehension

Date 2014-09-23 16:01 -0600
Subject Best way to deal with different data types in a list comprehension
From Larry Martell <larry.martell@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.14290.1411509701.18130.python-list@python.org> (permalink)

Show all headers | View raw


I have some code that I inherited:

' '.join([self.get_abbrev()] +
           [str(f['value')
            for f in self.filters
            if f.has_key('value')]).strip()


This broke today when it encountered some non-ascii data.

I changed the str(f['value']) line to f['value'].encode('utf-8'),
which works fine, except when f['value'] is not a string (it could be
anything).

Without rewriting this without the list comprehension, how can I write
this to deal with both strings and non-strings?

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Best way to deal with different data types in a list comprehension Larry Martell <larry.martell@gmail.com> - 2014-09-23 16:01 -0600
  Re: Best way to deal with different data types in a list comprehension Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-24 23:16 +1000

csiph-web