Python 2 Dict_items.sort() In Python 3
I'm porting some code from Python 2 to 3. This is valid code in Python 2 syntax: def print_sorted_dictionary(dictionary): items=dictionary.items() items.sort() In Pyth
Solution 1:
Use items = sorted(dictionary.items())
, it works great in both Python 2 and Python 3.
Solution 2:
dict.items
returns a view instead of a list in Python 3 (somewhat similarly to the iteritems
method in Python 2.x). To get a sorted list of the items use
sorted_items = sorted(d.items())
The sorted
builtin takes an iterable and returns a new list of its items, sorted.
Post a Comment for "Python 2 Dict_items.sort() In Python 3"