Question

from dictionary to list comprehension

Posted April 28, 2021 39 views
PythonDevelopment

i want to know do we able to create dictionary into list via list comprehension....
and in

n_list=[letter for letter in name]

what actually these “letters” define..
what the core idea.....

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @Sky01,

Basically, you want to create a list from a dictionary, is that correct? Now, do you want to merge all keys and values of your dictionary into a list or create two separate lists, one for your keys and one for your values?

Let’s say you want to create one list containing both the key and values of your dictionary:

for key, value in dict.iteritems():
    temp = [key,value]
    dictlist.append(temp)

If you want to create different lists for both your key and your value, you can try :

# Create a list of keys
list(dict.keys())

# Create a list of values
list(dict.values())

Regards,
KFSys