How to get a one list for the keys and and another for the values of a dictionary in python?

Viewed 29

I have a dictionary and I would like to get one list for the keys and and another for the values using python

My dictionary:

mydict = {'epr1': 'Jack', 'epr2': 'Jane', 'epr3': 'John'}
2 Answers

I would do something like:


mydict = {'epr1': 'Jack', 'epr2': 'Jane', 'epr3': 'John'}
keysList = list(mydict.keys())
valuesList = list(mydict.values())
print(keysList)
print(valuesList)

keysList will get you the dictionary keys as a list and valuesList will get you the dictionary values as a list

Hope this helps

mydict = {'epr1': 'Jack', 'epr2': 'Jane', 'epr3': 'John'}
keys = list(mydict.keys())
values = list(mydict.values())
print(keys, values)

prints

['epr1', 'epr2', 'epr3'] ['Jack', 'Jane', 'John']
Related