Counting number of values in dictionary

This blog explain how to find number of values in a dictionary

Below lines contain dictionary of country code. It contain 7 key and value pair.

country_code = {'Argentina':54,
                'Australia':61,
                'Belgium':32,
                'China':86,
                'India':91,
                'Sweden':46,
                'Taiwan':886
}

Creating a variable called country_code_values and assigning only values from dictionary

country_code_values = country_code.values()

Printing all values from variable

print(country_code_values)

Using len() function to print number of values

print(len(country_code_values))

Output

dict_values([54, 61, 32, 86, 91, 46, 886])
7

Complete Code

country_code = {'Argentina':54,
                'Australia':61,
                'Belgium':32,
                'China':86,
                'India':91,
                'Sweden':46,
                'Taiwan':886
}
country_code_values = country_code.values()
print(country_code_values)
print(len(country_code_values))

Example 2 : Finding number of values in country capital dictionary

country_capital = {'France':'Paris',
                   'UK':'London',
                   'Germany':'Berlin',
                   'Egypt':'Cairo',
                   'Turkey':'Istanbul',
                   'India':'Delhi',
                   'China':'Beijin',
                   'Japan':'Tokyo'
}

country_capital_values = country_capital.values()
print(country_capital_values)
print(len(country_capital_values))

Output

dict_values(['Paris', 'London', 'Berlin', 'Cairo', 'Istanbul', 'Delhi', 'Beijin', 'Tokyo'])
8
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s