How to Count the Number of Keys in a Dictionary in Python?

In Python, dictionary data types are used to store data collection in key-value syntax. Every key in the dictionary has a unique value that can be accessed in a program using the specified key name. Python offers various methods to apply some operations on the keys of a dictionary.

This write-up will provide various methods to count the number of keys in the Python dictionary.

So, let’s begin!

Method 1: Using len() Function

The “len()” function is used to find the count of the number of keys in the input dictionary. In the example given below, the “len()” function returns the number of keys:

Code:

dict_value = {'Name' : 'Lily', 'Age': 22, 'Height': 5.3}
print("Total Numbers of Keys: ",len(dict_value))

In the above code, the len() function accepts the dictionary variable “dict_value” as an argument and returns the total number of keys.

Output:

The total number of keys has been printed on the screen using the “len()” function.

Method 2: Using User-Defined Function

We can also define the function in the program that counts the number of keys in a dictionary. In the example given below, the user-defined function for counting keys of the dictionary using the for loop is defined in the program:

Code:

def keyscounter(dict):
    z = 0
    for item in dict:
        z += 1
    return z
print(keyscounter({'Name' : 'Lily', 'Age': 22, 'Height': 5.3}))

In the above code, the function named “keyscounter” is defined in the program. The dictionary value is passed as a parameter value of the “keyscounter()” function, which retrieves the keys of the input dictionary.

Output:

The number count of dictionary keys is displayed on the screen using a user-defined function.

Method 3: Using for Loop

The “for” loop can also be used in the program to iterate over the dictionary keys and return the number of keys. The example code below shows this method’s detailed working:

Code:

dict_value = {'Name' : 'Lily', 'Age': 22, 'Height': 5.3}

count = 0
for key,value in dict.items(dict_value):
    count += 1
   
print("Total Numbers of Keys: ", count)

In the above code, the “for” loop iterates over the dictionary and returns keys. For each iteration in the dictionary, the count value has been incremented by “1”.

Output:

The total number of keys “3” has been returned by the program using for loop and counter.

How to Print the Names of Dictionaries in Python?

To print the dictionary’s name, the “dict.keys()” function has been utilized in Python. The “dict.keys()” function returns the names of the dictionary’s keys. let’s understand it via the following examples:

Code: (Printing the Key Names of Dictionary )

dict_value = {'Name' : 'Lily', 'Age': 22, 'Height': 5.3}
z = dict_value.keys()
print(z)

In the above code, the dictionary variable named “dict_value” is initialized in the program. The “dict_value.keys()” will return the key names of the input dictionary.

Output:

The key names of the input dictionary “dict_value” have been printed on the screen.

We can also count the number of dictionary keys using the “dict.keys()” function along with the “len()” function. Here is an example of how we can do that in a program:

Code: (Printing the Number of Dictionary Keys)

dict_value = {'Name' : 'Lily', 'Age': 22, 'Height': 5.3}
z = dict_value.keys()
print(z)
print("Total Numbers of Keys: ",len(z))

In the above code, the “dict.keys()” function is used to get the keys name of the dictionary, and the len() function is used to get the length or number count of dictionary keys.

Output:

The key names of the dictionary and the number of keys in the dictionary are printed on the screen.

Conclusion

The “len()” function, user-defined function, and “for” loop are used to count the number of keys in the Python dictionary. The user-defined function is also defined using “for loop” to iterate over the dictionary’s keys and return the total number of dictionaries in Python. The “dict.keys()” function is used in Python to get the names of dictionary keys. This article has presented a detailed guide on counting the number of keys in a dictionary.