Re: Sorting dict keys

2007-07-22 Thread Martin v. Löwis
> I'd like to do it in one line because what I am trying to do is, after > all, a single, simple enough action. I find the suggested > b = sorted(a.keys()) much more readable than breaking it up in two > lines. I think you have demonstrated that a single-line statements with multiple functions a

Re: Sorting dict keys

2007-07-21 Thread zacherates
>From ESR's "Why python?" b = a.keys(); b.sort() Two statements, one line. Doesn't create an incidental copy the way sorted does but I'm not sure how much you like to jam on one line. Cheers, Aaron -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting dict keys

2007-07-21 Thread Bjoern Schliessmann
[EMAIL PROTECTED] wrote: > I'd like to do it in one line because what I am trying to do is, > after all, a single, simple enough action. No, it's two actions: 1. You extract a list of keys from the dict 2. you sort it > I find the suggested b = sorted(a.keys()) much more readable than > brea

Re: Sorting dict keys

2007-07-21 Thread montyphyton
On 21 srp, 02:31, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Fri, 20 Jul 2007 15:27:51 -0700, montyphyton wrote: > b = a.keys() > b.sort() > > [1, 2, 3] > > > Works fine, but I would really like it if I could somehow do it in one > > line. > > Why? Is the Enter key on your keyboard b

Re: Sorting dict keys

2007-07-20 Thread Steven D'Aprano
On Fri, 20 Jul 2007 15:27:51 -0700, montyphyton wrote: b = a.keys() b.sort() > [1, 2, 3] > > Works fine, but I would really like it if I could somehow do it in one > line. Why? Is the Enter key on your keyboard broken? Is there a global shortage of newline characters that I haven't be

Re: Sorting dict keys

2007-07-20 Thread Alex Popescu
Miles <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > On 7/20/07, Alex Popescu <[EMAIL PROTECTED]> wrote: >> If you just want to iterate over your dict in an ordered manner than >> all you have to do is: >> >> for k in my_dict.keys().sort(): >> # rest of the code > > I think you meant s

Re: Sorting dict keys

2007-07-20 Thread Roberto Bonvallet
On 20 jul, 19:34, [EMAIL PROTECTED] wrote: > copy.copy returns a new object: > > >>> copy.copy(a.keys()) > > [1,2,3] > > Then why doesn't copy.copy(a.keys()).sort() work?? It works, but you don't notice it, because you don't save a reference to the new list. Try this: c = copy.copy(a.keys())

Re: Sorting dict keys

2007-07-20 Thread montyphyton
On 21 srp, 00:47, Duncan Smith <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Consider the following: > > a = {1:2, 3:4, 2:5} > > > Say that i want to get the keys of a, sorted. First thing I tried: > > b = a.keys().sort() > print b > > > None > > > Doesn't work. Probably be

Re: Sorting dict keys

2007-07-20 Thread Roberto Bonvallet
On 20 jul, 18:50, Alex Popescu <[EMAIL PROTECTED]> wrote: > If you just want to iterate over your dict in an ordered manner than all > you have to do is: > > for k in my_dict.keys().sort(): > # rest of the code sort() returns None, so this code won't work either. -- Roberto Bonvallet -- http:

Re: Sorting dict keys

2007-07-20 Thread Miles
On 7/20/07, Alex Popescu <[EMAIL PROTECTED]> wrote: > If you just want to iterate over your dict in an ordered manner than all > you have to do is: > > for k in my_dict.keys().sort(): > # rest of the code I think you meant sorted(my_dict.keys()), since, as you just pointed out, the sort() method

Re: Sorting dict keys

2007-07-20 Thread Alex Popescu
[EMAIL PROTECTED] wrote in news:1184970471.146819.86280 @r34g2000hsd.googlegroups.com: I am not sure about your scenario, but as you discovered the sort() method is modifying the in place list (and doesn't return a new one). If you just want to iterate over your dict in an ordered manner than al

Re: Sorting dict keys

2007-07-20 Thread Duncan Smith
[EMAIL PROTECTED] wrote: > Consider the following: > a = {1:2, 3:4, 2:5} > > > Say that i want to get the keys of a, sorted. First thing I tried: > > b = a.keys().sort() print b > > None > > Doesn't work. Probably because I am actually trying to sort the keys > of the dictionary

Re: Sorting dict keys

2007-07-20 Thread Will Maier
On Fri, Jul 20, 2007 at 03:27:51PM -0700, [EMAIL PROTECTED] wrote: > Consider the following: > >>> a = {1:2, 3:4, 2:5} > > Say that i want to get the keys of a, sorted. First thing I tried: > > >>> b = a.keys().sort() > >>> print b > None list's sort() method sorts the list _in_place_: >>>

Re: Sorting dict keys

2007-07-20 Thread Jean-Paul Calderone
On Fri, 20 Jul 2007 15:27:51 -0700, [EMAIL PROTECTED] wrote: >Consider the following: a = {1:2, 3:4, 2:5} > >Say that i want to get the keys of a, sorted. First thing I tried: > b = a.keys().sort() print b >None > >Doesn't work. Probably because I am actually trying to sort the keys

Re: Sorting dict keys

2007-07-20 Thread Erik Max Francis
[EMAIL PROTECTED] wrote: > Consider the following: a = {1:2, 3:4, 2:5} > > Say that i want to get the keys of a, sorted. First thing I tried: > b = a.keys().sort() print b > None > > Doesn't work. Probably because I am actually trying to sort the keys > of the dictionary without

Sorting dict keys

2007-07-20 Thread montyphyton
Consider the following: >>> a = {1:2, 3:4, 2:5} Say that i want to get the keys of a, sorted. First thing I tried: >>> b = a.keys().sort() >>> print b None Doesn't work. Probably because I am actually trying to sort the keys of the dictionary without copying them first. If that is the case, fin