Let's say I have this dictionary:
d = {'a': 1, 'b': 2, 'c': 3}
So doing d.items(), it would convert it to:
[('a', 1), ('c', 3), ('b', 2)]
But I need it to be like this:
[('a', '=', 1), ('c', '=', 3), ('b', '=', 2)]
What would be most pythonic/efficient way of doing it? Are there better alternatives than just iterating over a dictionary and building tuples for every key/value pair?
You have to iterate over the dictionary and construct each tuple. Using a list comprehension, this is fairly straightforward to do:
>>> [(k, '=', v) for k, v in d.items()]
[('a', '=', 1), ('c', '=', 3), ('b', '=', 2)]
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