Can the data from the dictionary values() be used for thing other than iteration?


#1

Question

In this exercise, the results of the values() function are used in a for loop. Can the data friom values() be used in other ways?

Answer

The results from values() are not just for iteration. They can be used in any way that a list can. In the following example code, the values from a dictionary are passed to functions which can determine the minimum, maximum, and total values of the data.

cities = {"Los Angeles": 3971883,
          "San Diego": 1394928,
          "San Jose": 1026908}

print(sum(cities.values()))
print(max(cities.values()))
print(min(cities.values()))