• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Count Values by Key in Python Dictionary

Count Values by Key in Python Dictionary

May 18, 2022 Leave a Comment

To count the values by key in a Python dictionary, you can use comprehension to loop over the dictionary items, and then count the number of items for a given key with the Python len() function.

d = { "a":[1,2,3], "b":[1,2,3,4,5], "c":[1,2], "d":[1,2,3,4,5,6,7] }  

count = { k: len(v) for k, v in d.items() }

print(count)

#Output:
{'key1': 3, 'key2': 5, 'key3': 2, 'key4': 7}

In Python, dictionaries are a collection of key/value pairs separated by commas.

When working with dictionaries, sometimes we have keys which have values which are lists and we want to know the length of each of the lists for each key.

To count the values for each key in a dictionary using Python, we can use dictionary comprehension. With dictionary comprehension, we can loop over the key/value pairs and count the values for each key with len().

Below is a simple example which will count the values by key in a dictionary using Python.

d = { "a":[1,2,3], "b":[1,2,3,4,5], "c":[1,2], "d":[1,2,3,4,5,6,7] } 

count = { k: len(v) for k, v in d.items() }

print(count)

#Output:
{'a': 3, 'b': 5, 'c': 2, 'd': 7}

Using Python to Count Values by Key in Dictionary

The example above works if our dictionary is well structured and all of the keys have values of type list. If the values have mixed types, then using len() blindly might not give you the result you are looking for.

For example, if you have a dictionary where some values are lists and others are numbers, then you will want to only get the count of values for certain keys.

In this case, you can use an if statement to only return keys which have values of type list.

Let’s say we have the following dictionary.

d = { "a":[1,2,3], "b":2, "c":"Hello", "d":[1,2,3,4,5,6,7] } 

If you want to get the count of values for keys where the value is of type list, then you can use the following Python code.

d = { "a":[1,2,3], "b":2, "c":"Hello", "d":[1,2,3,4,5,6,7] }

count = { k: len(v) for k, v in d.items() if type(v) == list}

print(count)

#Output:
{'a': 3, 'd': 7}

Hopefully this article has been useful for you to learn how to count the values by key in a dictionary using Python.

Other Articles You'll Also Like:

  • 1.  Get Length of File Using Python
  • 2.  Using Python to Check if Number is Divisible by Another Number
  • 3.  Remove Every Nth Element from List in Python
  • 4.  Python atan2 – Find Arctangent of the Quotient of Two Numbers
  • 5.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 6.  Convert Set to List in Python
  • 7.  Using Python to Capitalize Every Other Letter of String
  • 8.  Create Empty String Variable in Python
  • 9.  Remove Extension from Filename in Python
  • 10.  Using Python to Insert Item Into List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy