Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create list from a dictionary?

I’m pretty new to python, and I want to create a list from a dictionary.

If I had a dictionary declared like this:

a = {"e1" : "Hello", "e2" : "Hi"}

I would want to get a list like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a = [["e1", "Hello"], ["e2", "Hi"]]

I honestly don’t know how I would even start to do this so I don’t have any code to help.

>Solution :

You can use a for loop in the dictionary .keys() method like this:

a = {"e1" : "Hello", "e2" : "Hi"}

b = []
for key in a.keys():
    b.append([key, a[keys]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading