Python len Dictionary function is used to return the dictionary length (the total key-value pairs). In this section, we discuss how to use this Python Dictionary len function. The syntax behind this len function is:
len(dictionary_name)
Python Dictionary len Example
The dictionary len function returns the length of a dictionary. The below Python code prints the myDict length.
# Python Dictionary len Example myDict = {1: 'apple', 2: 'Banana' , 3: 'Orange', 4: 'Kiwi'} print("Dictionary Items: ", myDict) # Dictionary length print("\nThe Dictionary length : ", len(myDict))
Python Dictionary len Example 2
In this program, we are going to insert new value to a Dictionary and check the length. Next, we updated a value and displayed the length.
# Python Dictionary len Example myDict = {'name': 'Kevin', 'age': 25} # Print Total Dictionary print("Dictionary Items : ", myDict) print("The Dictionary length : ", len(myDict)) # Add an Item to Dictionary myDict['job'] = 'Programmer' print("\nDictionary Items : ", myDict) print("The Dictionary length : ", len(myDict)) # Update an Item in Dictionary myDict['name'] = 'Python' print("\nDictionary Items : ", myDict) print("The Dictionary length : ", len(myDict))