Given a python a list of dictionary of key-value pairs i.e.
[{'color': 'red', 'value': 'high'}, {'color': 'yellow', 'value': 'low'}]
How to construct a list of tuples from the dictionary values only:
[('red', 'high'), ('yellow', 'low')]
As simple as it gets:
result = [(d['color'], d['value']) for d in dictionarylist]
If order is important then:
[tuple(d[k] for k in ['color', 'value']) for d in data]
Or:
[(d['color'], d['value']) for d in data]
Else without order guarantees or from an OrderedDict
(or relying on Py3.6 dict
):
[tuple(d.values()) for d in data]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With