If I have a dictionary like:
JavaScript
x
2
1
{'a': 1, 'b': 2, 'c': 3}
2
How can I convert it to this?
JavaScript
1
2
1
[('a', 1), ('b', 2), ('c', 3)]
2
And how can I convert it to this?
JavaScript
1
2
1
[(1, 'a'), (2, 'b'), (3, 'c')]
2
Advertisement
Answer
JavaScript
1
4
1
>>> d = { 'a': 1, 'b': 2, 'c': 3 }
2
>>> list(d.items())
3
[('a', 1), ('c', 3), ('b', 2)]
4
For Python 3.6 and later, the order of the list is what you would expect.
In Python 2, you don’t need list
.