How to count Number of Keys in Dictionary Python
Are you are learning about “How to count Number of Keys in Dictionary Python“? In today’s article, I will provide solutions for you to solve the issues. Please follow the below steps to get the problem resolved now
How to count the number of keys in Dictionary Python
To count Numbers of Keys within Dictionary Python, you only need use len(). You can use len() to count Numbers of Keys within Dictionary Python. Let’s take an example:dict1 = {'R1' : 45,'R2' : 48,'R3' : 50} print(len(dict1.keys()))
Output: 3
Method 1: Use len()
You can count the number of keys in Dictionary by using len() This is illustrated in the following example:
dict1 = {'R1' : 45,'R2' : 48,'R3' : 50}
print(len(dict1.keys()))
Output:
3
Method 2: Use enumerate()
You can count the number of keys by using enumerate() This is illustrated in the following example:
dict1 = {'R1' : 45,'R2' : 48,'R3' : 50}
def count_keys(dict):
count = 0
for i in enumerate(dict):
count += 1
return count
print(count_keys(dict1))
Output:
3
Final Words
The above are useful solutions for the question “How to count Number of Keys in Dictionary Python?”, if you can’t solve it well. Please leave a message.