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


Groups > comp.lang.python > #22607

Re: Is there a better way to do this snippet?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'lines.': 0.07; 'python': 0.09; 'if,': 0.09; 'tuple': 0.09; 'unpack': 0.09; 'cases': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wed,': 0.18; 'wrote:': 0.19; 'explicit': 0.21; 'header:In-Reply-To:1': 0.22; 'message- id:@mail.gmail.com': 0.24; 'leave': 0.26; 'embed': 0.28; 'received:209.85.210.174': 0.30; 'received:mail- iy0-f174.google.com': 0.30; 'skip:g 30': 0.30; 'chris': 0.31; 'received:209.85': 0.32; 'received:google.com': 0.32; 'received:209.85.210': 0.33; 'am,': 0.34; 'two': 0.35; 'to:addr :python-list': 0.35; 'received:209': 0.35; 'subject:?': 0.35; 'item': 0.37; 'list,': 0.37; 'subject:: ': 0.37; 'instead': 0.38; 'skip:i 20': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; '...': 0.40; '2012': 0.62; 'cut': 0.65; 'directly.': 0.71; 'subject:this': 0.84; 'subject:better': 0.84; 'subject:there': 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:content-transfer-encoding; bh=tcsqfYz0xHlsqplxaDDffGQRFdbiuzho9Q6ejGvI8vs=; b=g0QnUH3Ki7HJBJaFKA2wLqtekw5MJX8lfLfYIe0E8qFZV469x2IuoUyWKP+pFNQQwN ln3RJMqNqFOJJ3fdLjON9fLCBwEsfG5Hi0Fd9OsMrKh7TPJDONguiwhgQZRwYL8JV4My l0pw36bAFoFMQCDj/wNYSUNPpCj3dNszL97LXNWbFDJi9DV4ylVc1Ry0izkjdNTSzK9F aN3qB2bT1T6snjfAHr1waNSjoZ1RnIoqIj9fbhPGFo5oi5P0J9PH6WErdJip8t2jLL+y An0suL/qvpqbA76rIU8Zh2vBz8mupDyQd/etMGX1F9wtpcJKMzf5805rbZUTG9E1n3yX 4utw==
MIME-Version 1.0
In-Reply-To <52664090-0833-4ec2-af8b-326737dd67d1@w5g2000yqi.googlegroups.com>
References <52664090-0833-4ec2-af8b-326737dd67d1@w5g2000yqi.googlegroups.com>
Date Wed, 4 Apr 2012 01:09:35 +1000
Subject Re: Is there a better way to do this snippet?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1280.1333465778.3037.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333465779 news.xs4all.nl 6950 [2001:888:2000:d::a6]:44166
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22607

Show key headers only | View raw


On Wed, Apr 4, 2012 at 12:36 AM, python <w.g.sneddon@gmail.com> wrote:
> for item in tag23gr:
> ...     value, key = tuple(item)
> ...     if(g23tag.get(key)):
> ...             g23tag[key].append(value)
> ...     else:
> ...             g23tag[key] = [value]

Simple enhancement: Use setdefault. Instead of the if, just use:

g23tag.setdefault(key,[]).append(value)

That'll cover both cases in one.

You can leave off the explicit tuple construction; if item is a
two-element list, you can unpack it directly. You can also embed that
straight into your for loop:

for value,key in tag23gr:

Do both and you cut your loop down to two lines. Cool! :)

Chris Angelico

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


Thread

Is there a better way to do this snippet? python <w.g.sneddon@gmail.com> - 2012-04-03 07:36 -0700
  Re: Is there a better way to do this snippet? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-04-03 17:02 +0200
    Re: Is there a better way to do this snippet? nn <pruebauno@latinmail.com> - 2012-04-03 08:37 -0700
      Re: Is there a better way to do this snippet? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-04-03 18:26 +0200
        Re: Is there a better way to do this snippet? nn <pruebauno@latinmail.com> - 2012-04-03 11:03 -0700
  Re: Is there a better way to do this snippet? Chris Angelico <rosuav@gmail.com> - 2012-04-04 01:09 +1000
  Re: Is there a better way to do this snippet? Peter Otten <__peter__@web.de> - 2012-04-03 17:24 +0200

csiph-web