• 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 Number of Keys in Dictionary in Python

Count Number of Keys in Dictionary in Python

February 17, 2022 Leave a Comment

To find the number of keys in a dictionary in Python, the easiest way is to use the Python dictionary keys() function to get a list of the keys in a dictionary, and then get the length of that list with len().

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

number_of_keys_in_dictionary = len(dictionary.keys())

print(number_of_keys_in_dictionary)

#Output:
4

You can also use the Python len() function directly, as len() returns the number of key/value pairs in the dictionary.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

number_of_keys_in_dictionary = len(dictionary)

print(number_of_keys_in_dictionary)

#Output:
4

In Python, dictionaries are a collection of key/value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily get the number of keys in a dictionary.

To get the keys of a dictionary, we can use the keys() function. The keys() function returns a list of the keys of a dictionary.

Then, to count the number of keys in a dictionary in Python, we can use the Python len() function.

Let’s say we have the following dictionary in our Python code.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

To count the number of keys in our dictionary variable, we can call the keys() function, and then the len() function.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

number_of_keys_in_dictionary = len(dictionary.keys())

print(number_of_keys_in_dictionary)

#Output:
4

You can also use the Python len() function directly, as len() returns the number of key/value pairs in the dictionary and the length of the dictionary.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

number_of_keys_in_dictionary = len(dictionary)

print(number_of_keys_in_dictionary)

#Output:
4

As you can see above, there are 4 key/value pairs, and as a result, the len() function returns 4.

Using Enumeration to Count Number of Keys in Dictionary in Python

We can also use enumeration to count the number of keys in a dictionary. The Python enumerate() function allows us to iterate over the keys of a dictionary.

Below is an example in Python for how to count the number of keys in a dictionary.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

def count_keys(dict):
    count = 0
    for i in enumerate(dict):
        count += 1
    return count

print(count_keys(dictionary))

#Output:
4

Hopefully this article has been useful for you to learn how to use the Python keys() function, the Python len() function and the Python enumerate() function to get the count of keys in a dictionary.

Other Articles You'll Also Like:

  • 1.  PROC PHREG Equivalent in Python
  • 2.  Get First Day of Month Using Python
  • 3.  Convert String into Tuple in Python
  • 4.  How to Group By Columns and Count Rows in pandas DataFrame
  • 5.  Get Days of timedelta Object in Python
  • 6.  Run Something Every 5 Seconds in Python
  • 7.  How to Check if Number is Negative in Python
  • 8.  Get Day Name from Datetime in pandas DataFrame
  • 9.  List of Turtle Shapes in Python
  • 10.  Drop First Row of pandas DataFrame

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