Google News
logo
Python Program to Get the length of a Dictionary
You can use the `len()` function to get the length of a dictionary, which will return the number of key-value pairs in the dictionary. Here is an example program :
Program :
# Create a dictionary
my_dict = {'apple': 2, 'banana': 3, 'orange': 4}

# Get the length of the dictionary
dict_length = len(my_dict)

# Print the length of the dictionary
print("The length of the dictionary is:", dict_length)
Output :
The length of the dictionary is: 3
In the above program, we first create a dictionary called `my_dict` with three key-value pairs. We then use the `len()` function to get the length of the dictionary, and store the result in a variable called `dict_length`. Finally, we print the length of the dictionary using the `print()` function.