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


Groups > comp.lang.python > #63102

Re: Creating a list with holes

References <CACwCsY5P47-dB1NLQTUTQ=0aF6B+-M3y4hCxcUGmcVmHM8=-xQ@mail.gmail.com> <CAPTjJmp+NtGYWE3S3SGjo-xoYC74Exdpci_+x4AUNgqM5=CJNQ@mail.gmail.com> <CAGGBd_ooeJLsW_eoEBbrbiv9zxm_75bvWnO1U8JEj8xOnNwMfQ@mail.gmail.com> <CAPTjJmrbXLQP9gYwpzjQta6Mn78gDBQ39vFfsi7R7+42pMxeCw@mail.gmail.com>
Date 2014-01-03 19:32 -0800
Subject Re: Creating a list with holes
From Dan Stromberg <drsalists@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4880.1388806336.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jan 3, 2014 at 7:00 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Sat, Jan 4, 2014 at 1:58 PM, Dan Stromberg <drsalists@gmail.com> wrote:
>> On Fri, Jan 3, 2014 at 7:37 AM, Chris Angelico <rosuav@gmail.com> 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.

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


Thread

Re: Creating a list with holes Dan Stromberg <drsalists@gmail.com> - 2014-01-03 19:32 -0800

csiph-web