find the keys of a map in python

In Python, a Map is generally referred to as a Dictionary. You can find the keys of a dictionary in Python using the keys() function.

Here's an example:

main.py
# create a dictionary
my_dict = {"apple": 1, "banana": 2, "orange": 3}

# get the keys of the dictionary
keys = my_dict.keys()

# print the keys
print(keys)
157 chars
9 lines

This will output:

main.py
dict_keys(['apple', 'banana', 'orange'])
41 chars
2 lines

You can then iterate over the keys and print their values:

main.py
# iterate over the keys and print their values
for key in keys:
    print(my_dict[key])
88 chars
4 lines

This will output:

main.py
1
2
3
6 chars
4 lines

Alternatively, you can also convert the keys to a list using list() function.

main.py
# convert the keys to a list
key_list = list(my_dict.keys())

# print the list
print(key_list)
95 chars
6 lines

This will output:

main.py
['apple', 'banana', 'orange']
30 chars
2 lines

gistlibby LogSnag