Turn a dictionary into two lists, one for keys and the other for values?

dictionary, python

Solution

You can use in Python 2.x:

keys = tel.keys()
values = tel.values()

Or something funky like:

(keys,values) = zip(*tel.iteritems())

And in Python 3.x:

keys = list(tel.keys())
values = list(tel.values())
(keys,values) = zip(*tel.items())

(as in 3.x, keys(), values() and items() return iterators)

Problem

I read in a object of type `collections.defaultdict` from a file using `pickle.load`. It has a numerical value for each string key. It seems to be just like an object of type `dict`, or I might be wrong. I would like to create two lists out of the object, one list consisting all the keys, and the other list all the values. How can I do that for an object of type `dict`? How can I do that for an object of type `collections.defaultdict`? For example, from an object of type `dict` ``` tel = {'jack': 4098, 'sape': 4139} ``` I would like to create two lists `['jack', 'sape']` and `[4098, 4139]`. Thanks!!

Original source