
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Count number of items in a dictionary value that is a list in Python
We are given a Dictionary in which the values from the key value pair itself is a list. In this article we will see a how to count the number of items in this list which are present as values in the dictionary.
With isinstance
Hindi suppose we use isinstance function to find out if the value of the dictionary is a list. Then we increment a count variable whenever isinstance returns true.
Example
# defining the dictionary Adict = {'Days': ["Mon","Tue","wed","Thu"], 'time': "2 pm", 'Subjects':["Phy","Chem","Maths","Bio"] } print("Given dictionary:\n",Adict) count = 0 # using isinstance for x in Adict: if isinstance(Adict[x], list): count += len(Adict[x]) print("The number of elements in lists: \n",count)
Output
Running the above code gives us the following result −
Given dictionary: {'Days': ['Mon', 'Tue', 'wed', 'Thu'], 'time': '2 pm', 'Subjects': ['Phy', 'Chem', 'Maths', 'Bio']} The number of elements in lists: 8
With items()
Which items() we loop through each of the element of the dictionary and apply isinstance function to find out if it is a list.
Example
# defining the dictionary Adict = {'Days': ["Mon","Tue","wed","Thu"], 'time': "2 pm", 'Subjects':["Phy","Chem","Maths","Bio"] } print("Given dictionary:\n",Adict) count = 0 # using .items() for key, value in Adict.items(): if isinstance(value, list): count += len(value) print("The number of elements in lists: \n",count)
Output
Running the above code gives us the following result −
Given dictionary: {'Days': ['Mon', 'Tue', 'wed', 'Thu'], 'time': '2 pm', 'Subjects': ['Phy', 'Chem', 'Maths', 'Bio']} The number of elements in lists: 8
With enumerate
The enumerate function also expands and lists the items of a dictionary. We apply is instance to find out the values which are lists.
Example
# defining the dictionary Adict = {'Days': ["Mon","Tue","wed","Thu"], 'time': "2 pm", 'Subjects':["Phy","Chem","Maths","Bio"] } print("Given dictionary:\n",Adict) count = 0 for x in enumerate(Adict.items()): if isinstance(x[1][1], list): count += len(x[1][1]) print(count) print("The number of elements in lists: \n",count)
Output
Running the above code gives us the following result −
Given dictionary: {'Days': ['Mon', 'Tue', 'wed', 'Thu'], 'time': '2 pm', 'Subjects': ['Phy', 'Chem', 'Maths', 'Bio']} 8 The number of elements in lists: 8
- Related Articles
- How to count the number of items in a C# list?
- How to access nested Python dictionary items via a list of keys?
- Program to count number of elements in a list that contains odd number of digits in Python
- Associating a single value with all list items in Python
- Python – Remove dictionary from a list of dictionaries if a particular value is not present
- Python – Replace value by Kth index value in Dictionary List
- Python - Clearing list as dictionary value
- Python Program – Remove dictionary from a list of dictionaries if a particular value is not present
- Python Program to Multiply All the Items in a Dictionary
- Python program to find the sum of all items in a dictionary
- Python - Create nested list containing values as the count of list items
- Accessing Key-value in a Python Dictionary
- Python – All combinations of a Dictionary List
- Program to count average of all special values for all permutations of a list of items in Python
- Dictionary Methods in Python (cmp(), len(), items()…)
