Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'elegant': 0.07; 'expressions': 0.07; 'string': 0.09; 'lines.': 0.09; 'subject:skip:c 10': 0.09; 'things,': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'cc:name:python list': 0.16; 'mylist': 0.16; 'name)': 0.16; 'readable': 0.16; 'wrote:': 0.18; 'pfxlen:0': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'hi,': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'little': 0.38; 'then,': 0.60; 'name': 0.63; 'become': 0.64; 'great': 0.65; 'physical': 0.72; 'subject:this': 0.83 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 :cc:content-type; bh=aXkRx5JyrME/LYIZZ74bg1wpCTea5RNzD+rGfBEcZpo=; b=07XCH5s0m1vAyH9uYHSw762bzP5AAkCZFZEkn3q4DHiPj1dSou9D5c46J0jwSzqkAq +9pPRWkDf7xVcLzRtiISVmLe6wFq4Seevqd/2rWylNxb6/x8tXpu6WWon7YYdf1UgGQh s5pqvv5TZ+87rnm2s2ZBj5F6Si7eVYG4rBZRJgWbOlM8nipQyVxckKNuJQQwBuPiXZeR 6F4rfLqEytL4IfyMtDhRCFOyBpkhqXHmWObtSs9uCpLk1HNJR4rFMqx9HPaxwiQ0Dp+7 bNsTQU2zl7LQJBiIBwL4P4GcsyWWqpvACEtnjHq483Fnd/0NBhwTW4AhmCjh49T3miIH L0vw== MIME-Version: 1.0 X-Received: by 10.180.205.162 with SMTP id lh2mr577720wic.57.1390002561821; Fri, 17 Jan 2014 15:49:21 -0800 (PST) In-Reply-To: References: Date: Fri, 17 Jan 2014 15:49:21 -0800 Subject: Re: How to write this as a list comprehension? From: Dan Stromberg To: Piet van Oostrum Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390002563 news.xs4all.nl 2953 [2001:888:2000:d::a6]:40043 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64206 On Fri, Jan 17, 2014 at 3:19 PM, Piet van Oostrum wrote: > Hi, > > I am looking for an elegant way to write the following code as a list > comprehension: > > labels = [] > for then, name in mylist: > _, mn, dy, _, _, _, wd, _, _ = localtime(then) > labels.append(somefunc(mn, day, wd, name)) My recomendation: Don't use a list comprehension. List comprehensions and generator expressions are great for quick little things, but become less readable when you have to string them over multiple physical lines. > labels = [somefunc(mn, day, wd, name) > for then, name in mylist > for _, mn, dy, _, _, _, wd, _, _ in [localtime(then)]]