In this tutorial, we will discuss how to get the length of a dictionary in Python.
A Dictionary is a data structure that stores the data in the form of keys and values.
Key will be unique, and values can be the same. In Python, a dictionary is represented by {}.
Syntax:
{key:value,........................,key:value}
Where key and value can be anything like string or integer.
Also Read: How To Create An Empty Dictionary in Python
How to Get Python Dictionary Length
Here, we will use the len() function to find the length of the dictionary. It will count all items(key: value pairs) and return the count.
Syntax:
len(my_dictinary)
Where my_dictionary is the input dictionary.
Example:
In this example, we will create a dictionary with 5 items and get the length.
#create a dictinary my_dictinary={1:'php',2:'html',3:'jsp',4:'python',5:'R'} #display the dictionary print(my_dictinary) #get the length print(len(my_dictinary))
Output:
{1: 'php', 2: 'html', 3: 'jsp', 4: 'python', 5: 'R'} 5
Also Read: How To Concatenate Arrays in Python [With Examples]
We can also use items() to get the length of a dictionary.
Items () will return the key-value pair.
Syntax:
len(my_dictinary.items())
Where my_dictionary is the input dictionary.
Example:
In this example, we will create a dictionary with 5 items and get the length.
#create a dictinary my_dictinary={1:'php',2:'html',3:'jsp',4:'python',5:'R'} #display the dictionary items print(my_dictinary.items()) #get the length of dictionary items print(len(my_dictinary.items()))
Output:
dict_items([(1, 'php'), (2, 'html'), (3, 'jsp'), (4, 'python'), (5, 'R')]) 5
Also Read: What Does The Python Percent Sign Mean?
Get the length of keys in a dictionary
Here, we will use the len() function to find the length of keys. It will count all keys and return the count. We will get the keys by using the keys() function.
Syntax:
len(my_dictinary.keys())
Where my_dictionary is the input dictionary.
Example:
In this example, we will create a dictionary with 5 items and get the length.
#create a dictinary my_dictinary={1:'php',2:'html',3:'jsp',4:'python',5:'R'} #display the dictionary keys print(my_dictinary.keys()) #get the length of dictionary keys print(len(my_dictinary.keys()))
Output:
dict_keys([1, 2, 3, 4, 5]) 5
Also Read: How to Create an Empty Array In Python
Get the length of values in a dictionary
Here, we will use the len() function to find the length of values. It will count all values and return the count. We will get the values by using the values() function.
Syntax:
len(my_dictinary.values())
Where my_dictionary is the input dictionary.
Example:
In this example, we will create a dictionary with 5 items and get the length.
#create a dictinary my_dictinary={1:'php',2:'html',3:'jsp',4:'python',5:'R'} #display the dictionary values print(my_dictinary.values()) #get the length of dictionary values print(len(my_dictinary.values()))
Output:
dict_values(['php', 'html', 'jsp', 'python', 'R']) 5
Also Read: What Is Python Used For? Applications of Python
Get the length of an empty dictionary
Example:
We will create an empty dictionary and get the length in this example.
#create an empty dictinary my_dictinary={} #display the dictionary items print(my_dictinary.items()) #get the length of dictionary items print(len(my_dictinary.items())) #display the dictionary keys print(my_dictinary.keys()) #get the length of dictionary keys print(len(my_dictinary.keys())) #display the dictionary values print(my_dictinary.values()) #get the length of dictionary values print(len(my_dictinary.values()))
Output:
dict_items([]) 0 dict_keys([]) 0 dict_values([]) 0
Also Read: How To Display A Calendar In Python
Get the length of a nested dictionary
nested dictionary means a dictionary inside a dictionary. We can assign a dictionary as a value to a key.
Example:
We will create a nested dictionary in this example and get the length.
#create an nested dictinary with 5 items my_dictinary={1:{'php','java'},2:{'cpp','html'},3:'bigdata',4:'iot',5:'html'} #display the dictionary items print(my_dictinary.items()) #get the length of dictionary items print(len(my_dictinary.items())) #display the dictionary keys print(my_dictinary.keys()) #get the length of dictionary keys print(len(my_dictinary.keys())) #display the dictionary values print(my_dictinary.values()) #get the length of dictionary values print(len(my_dictinary.values()))
Output:
dict_items([(1, {'java', 'php'}), (2, {'cpp', 'html'}), (3, 'bigdata'), (4, 'iot'), (5, 'html')]) 5 dict_keys([1, 2, 3, 4, 5]) 5 dict_values([{'java', 'php'}, {'cpp', 'html'}, 'bigdata', 'iot', 'html']) 5
Also Read: How To Sort a List of Tuples in Python
Summary
In this tutorial, we explored how to find the length of a dictionary in Python using the len() method. We also explored how to get the length of keys in a dictionary and how to get the length of a nested dictionary.
Must check Python tutorials:
- Arithmetic Operators in Python [With Examples]
- How to Create Python Empty Set
- Python Matrix Using Numpy [With Examples]
- How to Exit Python Program [4 Methods]
- How to Convert Python Tuples to Lists
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator.
With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!