find the variance of all values in a map in python

To find the variance of all values in a map in Python, you can do the following steps:

  1. Import the statistics module which contains the variance method.
  2. Use the values() method to get a list of all the values in the map.
  3. Pass the list of values to the variance() method to calculate the variance.

Here's the code:

main.py
import statistics

my_map = {'a':2, 'b':5, 'c':8, 'd':4}

variance = statistics.variance(my_map.values())

print("Variance of all values in the map:", variance)
161 chars
8 lines

In this example, the output would be:

main.py
Variance of all values in the map: 5.6875
42 chars
2 lines

gistlibby LogSnag