-2

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:

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.

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Have you done any *research* to help you figure it out on your own? There are *many* online researches. A SO question should *not* be your first stop. – S3DEV Feb 19 '22 at 19:20

2 Answers2

1

Since you want the keys and values, iterate through a.items()
If you want just the keys: a.keys()
The values: a.values()

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

b = []
for key, value in a.items():
    b.append([key, value])
Hackerman
  • 46
  • 4
0

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]])
Timmy Diehl
  • 409
  • 3
  • 16