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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <larry.martell@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.049
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'string': 0.09; 'broke': 0.09; 'subject:skip:c 10': 0.09; '(it': 0.16; 'non-ascii': 0.16; 'rewriting': 0.16; 'to:name:python-list@python.org': 0.22; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'fine,': 0.31; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'subject:data': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'how': 0.40; 'today': 0.64; 'subject:Best': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=jmApsNCFTDwdC/l29vgdexeMEkoecAbN74leRcGAf0Y=; b=sgfBy6gTyGlrPW94nSMoYIVKndUc21fojQzj44hIpX5u5CCPC7AeL5QL2gXN60ogAJ 9+VlThoxlK1shUPA5vGfPxe70Snmq3HCwk6vO4LnD24YUzikY5d7bWT5icokkb+d5OuI UyUdyVYPixRs2ZOao9iHJleDU+vQWwG7J4W90la6XyxGDwR54tQxmJliZ1CQ5U3Fvs4A XjdN8d333ad349gBaeN6aIninXWnsrniZkLTOSluNJJK/frPG3DES5QTISmT2jqAc4E2 aV4bxQBMQ0taAyKOXalN8yA1jalwHrPZHZlVeESVFbLgB5CesyTbqlSIBMIi7kl+8TV1 jEkg==
MIME-Version 1.0
X-Received by 10.194.157.230 with SMTP id wp6mr2875245wjb.15.1411509694202; Tue, 23 Sep 2014 15:01:34 -0700 (PDT)
Date Tue, 23 Sep 2014 16:01:34 -0600
Subject Best way to deal with different data types in a list comprehension
From Larry Martell <larry.martell@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14290.1411509701.18130.python-list@python.org> (permalink)
Lines 16
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1411509701 news.xs4all.nl 2909 [2001:888:2000:d::a6]:47796
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78253

Show key headers only | 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