Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'list?': 0.07; 'dan': 0.09; 'iterate': 0.09; 'similar,': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'key)': 0.16; 'rarely': 0.16; 'sorting': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'pointed': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'sort': 0.25; 'script': 0.25; 'order.': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'fine,': 0.31; 'idea,': 0.31; 'lists': 0.32; 'probably': 0.32; 'run': 0.32; 'fri,': 0.33; 'subject:with': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'pm,': 0.38; 'to:addr:gmail.com': 0.65; 'worth': 0.66; '7:00': 0.84; 'dict.': 0.84; 'good:': 0.84 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=BGy6gv/rXdH3VNasYHTVdHDlRwUi2w9Ofl+Eda8wYMQ=; b=f35mF7YPp0AHQBP+Rg/tLXxt34kvM+5Av/D1Ci7LAGC2wvXuqjk3HFtQZDrtZ9twQs ith8MhtEnCX0W2HwOrhv/SCVHS2jGCSp+X3YPXuhqmHotoMhDl0e2nsfJ+dn/moDBxy4 UpCPmcxUO0aRzJJ+OXvnPV2uiNWaadYTIibIRTTiSruCEGuDkIfp/n7hU31PsTMpnYsW aO05CsOeUZ9BN8XgmX8MNxWTfnOaH+KfS4P8byRTfydeQSqlJR+FEhltwhz1UuGoh988 wlWbE2vcBCdLsI7ffnOCguTbJvTOWXDMCJ75fo8l5/zj31zLe4hLtZwGdff5kjcCoQR5 cf2g== MIME-Version: 1.0 X-Received: by 10.194.189.132 with SMTP id gi4mr59433820wjc.5.1388806333484; Fri, 03 Jan 2014 19:32:13 -0800 (PST) In-Reply-To: References: Date: Fri, 3 Jan 2014 19:32:13 -0800 Subject: Re: Creating a list with holes From: Dan Stromberg To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: "python-list@python.org" 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388806336 news.xs4all.nl 2846 [2001:888:2000:d::a6]:46378 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63102 On Fri, Jan 3, 2014 at 7:00 PM, Chris Angelico wrote: > On Sat, Jan 4, 2014 at 1:58 PM, Dan Stromberg wrote: >> On Fri, Jan 3, 2014 at 7:37 AM, Chris Angelico wrote: >>> Depending on what exactly you need, it's probably worth just using a >>> dict. In what ways do you need it to function as a list? You can >>> always iterate over sorted(some_dict.keys()) if you need to run >>> through them in order. >> >> FWIW, sorting inside a loop is rarely a good idea, unless your lists >> are pretty small. > > What do you mean by "sorting inside a loop"? I was thinking of this: > > for key in sorted(some_dict.keys()): > # blah blah > > which will sort once and then iterate over it. That is fine, sorting once at then end of a script is a good use of sorted(some_dict.keys()). However, it probably should be pointed out that this, while similar, is not so good: for thing in range(n): for key in sorted(some_dict.keys()): do_something(thing, key) ...because it's sorting n times.