Tuple list from dict in Python

Manuel Araoz picture Manuel Araoz · Aug 18, 2009 · Viewed 66.5k times · Source

How can I obtain a list of key-value tuples from a dict in Python?

Answer

Andrew Keeton picture Andrew Keeton · Aug 18, 2009

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())