Tuple list from dict in Python [duplicate]
Solution 1:
For Python 2.x only (thanks Alex):
yourdict = {}
# ...
items = yourdict.items()
See http://docs.python.org/library/stdtypes.html#dict.items for details.
For Python 3.x only (taken from Alex's answer):
yourdict = {}
# ...
items = list(yourdict.items())
Solution 2:
For a list of of tuples:
my_dict.items()
If all you're doing is iterating over the items, however, it is often preferable to use dict.iteritems()
, which is more memory efficient because it returns only one item at a time, rather than all items at once:
for key,value in my_dict.iteritems():
#do stuff