In this lesson we will learn about Python dictionary len method. This len method will return the number of elements in the dictionary.
Python dictionary len Method
Python dictionary len method is an effective way to get the number of elements in a dictionary. The len method can also be used for getting number of elements in Tuple and List.
Syntax
len( myDictionary )
- myDictionary – that specific Dictionary of which you want to get the number of elements.
This method will return the number of elements.
Examples
Here are some examples of Python dictionary len method:
myDict1 = { 'name': 'John Doe', 'email': 'john@email.com', 'grade': 3.6, 'level': 8} myDict2 = { 'color': 'black', 'position': 'top' } print " Length of myDict1 is : ", len( myDict1 ) print " Length of myDict2 is : ", len( myDict2 )
If you run this script then the output will look like this:
Length of myDict1 is : 4 Length of myDict2 is : 2
You can also use the len() method for getting number of elements in Tuple, List, even String.
The post Python dictionary len Method appeared first on JS Tricks.