Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'iterate': 0.09; 'message-id:@stoneleaf.us': 0.09; 'subject:extra': 0.09; '~ethan~': 0.09; 'python': 0.11; 'def': 0.12; 'background.': 0.14; '1):': 0.16; 'igor': 0.16; 'iterating': 0.16; 'shorten': 0.16; 'string:': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'header:User-Agent:1': 0.23; 'lets': 0.24; 'looks': 0.24; 'source': 0.25; 'second': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'dec': 0.30; "i'm": 0.30; 'code': 0.31; 'agreed.': 0.31; 'directly,': 0.31; 'fri,': 0.33; 'skip:d 20': 0.34; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:173': 0.61; 'different': 0.65; 'me!': 0.84; 'received:gateway14.websitewelcome.com': 0.84; 'joel': 0.91; '2013': 0.98 Date: Fri, 06 Dec 2013 15:49:18 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Eliminate "extra" variable References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3304.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source-IP: 173.12.184.233 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:47148 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20= 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386377508 news.xs4all.nl 2861 [2001:888:2000:d::a6]:44028 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61207 On 12/06/2013 03:38 PM, Joel Goldstick wrote: > On Fri, Dec 6, 2013 at 2:37 PM, Igor Korot wrote: >> >> def MyFunc(self, originalData): >> data = {} >> dateStrs = [] >> for i in xrange(0, len(originalData)): >> dateStr, freq, source = originalData[i] >> data[str(dateStr)] = {source: freq} >> >> # above line confuses me! >> >> dateStrs.append(dateStr) >> for i in xrange(0, len(dateStrs) - 1): >> currDateStr = str(dateStrs[i]) >> nextDateStrs = str(dateStrs[i + 1]) > > Python lets you iterate over a list directly, so : > > for d in originalData: > dateStr, freq, source = d > data[source] = freq You could shorten that to for dateStr, freq, source in originalData: and if dateStr is already a string: data[dateStr] = {source: freq} > Your code looks like you come from a c background. Python idioms are different Agreed. > I'm not sure what you are trying to do in the second for loop, but I think you are trying to iterate thru a dictionary > in a certain order, and you can't depend on the order The second loop is iterating over the list dateStrs. -- ~Ethan~