Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python converting a dictionary's keys to a list

Python converting a dictionary's keys to a list

Document Object Reference Manual Python3 Examples

We can use list() function to convert a dictionary's keys to a list. The dictionary's keys can be obtained via the keys() method.

Example

my_dict

=

{

'name'

:

'Alice'

,

'age'

:

25

,

'city'

:

'New York'

}

keys_list

=

list

(

my_dict.

keys

(

)

)

print

(

keys_list

)

Code analysis:

Output result:


['name', 'age', 'city']

Document Object Reference Manual Python3 Examples