Aufwind
Aufwind

Reputation: 26288

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?

Upvotes: 31

Views: 74174

Answers (2)

Constantinius
Constantinius

Reputation: 35089

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

keys, values = my_dict.keys(), my_dict.values()

Upvotes: 45

FogleBird
FogleBird

Reputation: 76872

keys, values = zip(*d.items())

Upvotes: 89

Related Questions