Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'skip:[ 20': 0.04; 'string': 0.09; 'broke': 0.09; 'subject:skip:c 10': 0.09; '(it': 0.16; '23,': 0.16; 'non-ascii': 0.16; 'rewriting': 0.16; 'skip:[ 30': 0.16; 'wrote:': 0.18; 'to:name:python-list@python.org': 0.22; 'unicode': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'fine,': 0.31; 'larry': 0.31; 'post.': 0.31; 'sep': 0.31; 'worked': 0.33; 'maybe': 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:data': 0.36; 'thanks': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'how': 0.40; 'today': 0.64; 'different': 0.65; 'subject:Best': 0.91; 'reply,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=K0JYBGyPVA+x7oswEmeTJRhtJh9q+i8EAmty+Pl/WPA=; b=suziyqMKBl5IYeBfYjw9s+/KKSMVF1/66nS/RsYosq11C6jiR1kUBB5r5pxmu8zKgz X1sZfKjuWbI/9hYP0SHM0QnIUCU19P3JyMyRY8+hshKx7p14AIUCZ4zFTRxmahxUapTh 7R6Whka3AEzQosAizz9LPpbm/WzoFggD5qhsEdBtjmHKLmhG5ZfHXJcnm+sL9uL19ZUT Du8OHpLVyCRJq3F/1c+NfmgHyDQWVqpb11cmrL1VXaqLnv/fBrPEskjsIHkPqYSPqKcK adBr2qkR7WO3WqGiTTHClY7HMuQtZyBHw3R4cgqSLDY7qV+1HTTL+zM6yQ7XFM6DFn/y 5NGA== MIME-Version: 1.0 X-Received: by 10.180.208.13 with SMTP id ma13mr6524994wic.53.1411510806679; Tue, 23 Sep 2014 15:20:06 -0700 (PDT) In-Reply-To: References: Date: Tue, 23 Sep 2014 18:20:06 -0400 Subject: Re: Best way to deal with different data types in a list comprehension From: Larry Martell To: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411510813 news.xs4all.nl 2850 [2001:888:2000:d::a6]:56245 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78257 On Tue, Sep 23, 2014 at 6:05 PM, Rock Neurotiko wrote: > 2014-09-24 0:01 GMT+02:00 Larry Martell : >> >> 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? > Maybe there are a different way, but you can do this: > > ' '.join([self.get_abbrev()] + > [str(f['value').encode('utf-8') if type(f['value']) is str else > str(f['value'] > for f in self.filters > if f.has_key('value')]).strip() Thanks for the reply, but please don't top post. This worked for me: '.join([self.get_abbrev()] + [f['value'].encode('utf-8') if type(f['value']) is unicode else str(f['value']) for f in self.filters if f.has_key('value')]).strip()