Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more elegant way for unpacking keys and values of a dictionary into two lists, without losing consistence?

What I came up with is:

keys, values = zip(*[(key, value) for (key, value) in my_dict.iteritems()])

But I am not satisfied. What do the pythonistas say?

like image 411
Aufwind Avatar asked Sep 02 '25 14:09

Aufwind


2 Answers

keys, values = zip(*d.items())
like image 118
FogleBird Avatar answered Sep 05 '25 03:09

FogleBird


What about using my_dict.keys() and my_dict.values()?

keys, values = my_dict.keys(), my_dict.values()
like image 25
Constantinius Avatar answered Sep 05 '25 02:09

Constantinius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!