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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Apr 4, 2012 at 12:36 AM, python wrote: > for item in tag23gr: > ... =A0 =A0 value, key =3D tuple(item) > ... =A0 =A0 if(g23tag.get(key)): > ... =A0 =A0 =A0 =A0 =A0 =A0 g23tag[key].append(value) > ... =A0 =A0 else: > ... =A0 =A0 =A0 =A0 =A0 =A0 g23tag[key] =3D [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