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

Larry Martell larry.martell at
Wed Sep 24 00:01:34 CEST 2014

I have some code that I inherited:

' '.join([self.get_abbrev()] +
            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

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

More information about the Python-list mailing list