Python: How to convert a dictionary into a list of key-value

Using the list() function, we can convert a dictionary into a list of key-value tuple.

>>> x = {'a':1, 'b':2}
>>> x
{'a': 1, 'b': 2}
>>> y = list(x.items())
>>> y
[('a', 1), ('b', 2)

To convert back into dictionary, use the dict() function.

>>> z = dict(y)
>>> z
{'a': 1, 'b': 2}
If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Related

This entry was posted in Python and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>