```
python 3.2
>>>
[(i, sum(v) //len(v)) for i,v in t.items()]
```

Last Updated : 01 Aug, 2020

```
The original dictionary is: {
'Gfg': 4,
'is': 7,
'Best': 8,
'for': 6,
'Geeks': 10
}
The computed mean: 7.0
```

In this article we will discuss how to use values() function of dict class in python and then we will see several examples of the values() function.,As values() function returns a sequence of values of the dictionary, so we can pass this sequence to the sum() function to get the sum of all values of the dictionary. For example,,If we first fetch all the values of the dictionary using values() function and then modify the dictionary, then changes will be reflected in the sequence of the already fetched values too. For example,,To create a list of all values in the dictionary we can pass the sequence returned by values() function to the list() i.e.

In python, the dict class provides a member function to fetch all values from dictionary i.e.

dict.values()

## Get all values from a dictionary in python

# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, 'hi': 99 } # Get all values from a dictionary in python values = word_freq.values() print(values)

**Output:**

`dict_values([56, 23, 43, 78, 99])`

If we first fetch all the values of the dictionary using values() function and then modify the dictionary, then changes will be reflected in the sequence of the already fetched values too. For example,

# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, 'hi': 99 } # Get all values from a dictionary in python values = word_freq.values() print('Values of dictionary: ') print(values) # Modify the value of a key, it will modify the values # in already fetched sequence too word_freq['at'] = 200 print('Values of dictionary: ') print(values)

Output:

```
Values of dictionary:
dict_values([56, 23, 43, 78, 99])
Values of dictionary:
dict_values([56, 200, 43, 78, 99])
```

Get full access to Python Cookbook and 60K+ other titles, with free 10-day trial of O'Reilly.,Get Python Cookbook now with the O’Reilly learning platform.,You need a dictionary that maps each key to multiple values. ,The has_key_with_some_values function shown earlier also works for the second approach, and you also have analogous alternatives, such as:

Now in Python, the way to get a value from a dictionary is by using dictionary_name[key].,When we write lloyd["name"] we get his name, which is really just the value that corresponds to the key "name". Similarly, when we write lloyd["homework"] we get the list of grades that corresponds with the key "homework".,When you try to print this function though, you get an error. This makes me thing this solution isn’t correct, unless I am simply printing wrong?,i think the loop code should be this :-for key in sdict: h = average(key[“homework”]) q = average(key[“quizzes”]) t = average(key[“tests”]) because we are storing value stored in dictionary named sdict in “key” in each loop

I did “How is Everybody Doing?” - but I don’t really get the logic behind it O_o.

```
lloyd = {
"name": "Lloyd",
"homework": [90, 97, 75, 92],
"quizzes": [88, 40, 94],
"tests": [75, 90]
}
def average(some):
return sum(some) / len(some)
def get_class_average(student):
total = 0
for x in student:
total += get_average(x)
return total / len(student)
```

So i use something like `average(lloyd['homework'])`

and get the average score of Lloyds homework -that I understand: (sum of all homework elements)/by the number of elements.
but could somebody explain how it works in

```
for x in student:
total += get_average(x)
return total / len(student)
```

Now back to the code. First I’m going to re-write it a little.

```
students = [lloyd, alice, tyler]
for student in students:
total += get_average(student)
return float(total) / len(students)
```

Hopefully you will immediately see the difference but just to explain I’m going to make a chart. (pretend that the grades weren’t weighted)

```
student get_average(student)
lloyd average(lloyd["homework"]) + average(lloyd["tests"]) + average(lloyd["quizzes"])
alice average(alice["homework"]) + average(alice["tests"]) + average(alice["quizzes"])
tyler average(tyler["homework"]) + average(tyler["tests"]) + average(tyler["quizzes"])
```

This one worked for me and was quite simple.

```
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
```

My code

```
def get_class_average(classlist):
results = []
for student in classlist:
result = get_average(student)
results.append(result)
return average(results)
```

Here’s my code:

```
def get_class_average(students):
results = []
for item in students:
score = get_average(item)
results.append(score)
return average(results)
```

def main():
print (“This program will get input and return weighted average.”)
print ()

lloyd = {
“name”: “Lloyd”,
“homework”: [90.0, 97.0, 75.0, 92.0],
“quizzes”: [88.0, 40.0, 94.0],
“tests”: [75.0, 90.0]
}
alice = {
“name”: “Alice”,
“homework”: [100.0, 92.0, 98.0, 100.0],
“quizzes”: [82.0, 83.0, 91.0],
“tests”: [89.0, 97.0]
}
tyler = {
“name”: “Tyler”,
“homework”: [0.0, 87.0, 75.0, 22.0],
“quizzes”: [0.0, 75.0, 78.0],
“tests”: [100.0, 100.0]
}

```
students = [lloyd, alice, tyler]
results = get_class_average(students)
finalAvg = average(results)
print("The entire class average is: ", format(finalAvg, '.2f'))
```