[Tutor] Is there a programmatic use for keys() and values()

Steven D'Aprano steve at pearwood.info
Sun Jun 16 02:36:58 CEST 2013


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.





-- 
Steven


More information about the Tutor mailing list