find the average of all keys in a map in python

You can find the average of all keys in a map (dictionary) in python by using the sum and len functions. Here's an example:

main.py
my_map = {'key1': 10, 'key2': 20, 'key3': 30}

average_keys = sum(my_map.keys()) / len(my_map)

print("Average of keys:", average_keys)
136 chars
6 lines

This code will output:

main.py
Average of keys: 2.0
21 chars
2 lines

Note: In python 3.x, the keys() method returns a dictionary view object, which is iterable but not a list. So, we use the sum function to sum up all the keys and then divide by the length of the dictionary to find the average.

gistlibby LogSnag