[Tutor] Is there a programmatic use for keys() and values()
Dave Angel
davea at davea.name
Sun Jun 16 03:53:20 CEST 2013
On 06/15/2013 08:36 PM, Steven D'Aprano wrote:
> On 16/06/13 07:55, Alan Gauld wrote:
>> On 15/06/13 20:54, Jim Mooney wrote:
>>
>>> I just like to avoid typing all those odd little-finger characters.
>>> The dictionaries are the worst.
>>
>> I think your making it harder than it is.
>> Just use the result as you would expect and it will work.
>>
>> Don't get hung up over a list versus an iterable.
>> Just use it as is, mostly it will just do what you expect.
>
>
> Well, sometimes.
>
>
> for key in sorted(mydict.keys()):
> ...
>
>
> works fine. On the other hand:
>
> keys = mydict.keys()
> keys.sort()
> for key in keys:
> ...
>
>
> does not.
>
The sort() method doesn't work, but sorted does.
keys = mydict.keys()
for key in sorted(keys):
Or more directly,
for key in sorted(mydict.keys()):
--
DaveA
More information about the Tutor
mailing list