Thursday, October 16, 2008

Dictionary Methods

Straight from the list of changes in 3.0 regarding dictionary methods:

dict methods dict.keys(), dict.items() and dict.values() return views instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead.

Never fear! These methods can still give you lists, by using the list() function on them.

Example:
{'k':49,'g':500}.values() => [49,500] in 2.x
list({'k':49,'g':500}.values()) => [49,500] in 3.0.

This refactoring is included with the 2to3 shell script included with version 2.6 and version 3.0RC1

No comments: