﻿ Python: Total length of all values of a given dictionary with string values - w3resource

# Python: Total length of all values of a given dictionary with string values

## Python dictionary: Exercise-65 with Solution

Write a Python program to get the total length of all values in a given dictionary with string values.

Visual Presentation:

Sample Solution:

Python Code:

``````# Define a function 'test' that takes a dictionary 'dictt' as an argument.
def test(dictt):
# Use a generator expression to calculate the sum of lengths of all values in the input dictionary.
result = sum((len(values) for values in dictt.values()))
return result

# Create a dictionary 'color' with color codes as keys and color names as values.
color = {'#FF0000':'Red', '#800000':'Maroon', '#FFFF00':'Yellow', '#808000':'Olive'}

# Print a message indicating the start of the code section and display the original dictionary.
print("\nOriginal dictionary:")
print(color)

# Print a message indicating the purpose and demonstrate the 'test' function's usage
# to calculate the total length of all string values in the dictionary.
print("\nTotal length of all values of the said dictionary with string values:")
print(test(color))
```
```

Sample Output:

```Original dictionary:
{'#FF0000': 'Red', '#800000': 'Maroon', '#FFFF00': 'Yellow', '#808000': 'Olive'}

Total length of all values of the said dictionary with string values:
20
```

Flowchart:

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿