To get the length of dictionary values in Python, you can use the len() function, item() function, and the for loop. Follow the article to understand better.
Get the length of dictionary values in Python
Use the len() function
To get the length of the dictionary value, you need to use the len() function.
Syntax:
len(obj)
Parameters:
- obj: object to get the length.
Example:
- Create a list.
- Use the len() function to get the length of the value corresponding to the Key in the dictionary.
myDict = {'name': 'John', 'age': 18, 'occupation': 'developer'} # Use the len() function to get the length of the Value in the dictionary print("The length of myDict['Name']):", len(myDict["Name"]))
Output:
The length of myDict['Name']): 4
For a nested list, the value length will be represented as follows.
Example:
myDict = { 'name': 'John', 'age': 18, 'occupation': 'developer', 'habits': [ 'read books', 'play soccer'] } # Use the len() function to get the length of the Value in the dictionary print("The length of myDict['habits']: ", len(myDict["habits"]))
Output:
The length of myDict['habits']: 2
Use the item() function
You can use the item() function that returns the key/value pairs in the dictionary in conjunction with the isinstance() function.
Syntax:
dictionary.items()
Parameters: no parameters.
Example:
- Create a list.
- Declare a counter variable whose value is 0.
- Use the item() function to return the key/value pairs of the list.
- Using the isinstance() function has the effect of checking which data type the variable ‘val’ is, if the correct data type is to be determined, the counter variable will be increased by 1.
myDict = { 'name': 'John', 'age': 18, 'occupation': 'developer', 'habits': ['read books', 'play soccer'] } countVariable = 0 # Use the item() function for key, val in myDict.items(): # Check the data type of Value # If it is integer, the counter is incremented by 1 if isinstance(val, int): countVariable += 1 # Check the data type of Value # If it is string, the counter is incremented by 1 elif isinstance(val, str): countVariable += 1 else: countVariable += len(val) print("The total length of value is:", countVariable)
Output:
The total length of value is: 5
Use the for loop
You can use the for loop to get the length of dictionary values.
Example:
- Create a list.
- Declare a counter variable whose value is 0.
- Use a for loop to iterate through the values of the dictionary.
- Using the isinstance() function has the effect of checking which data type the iterator is; if the correct data type is to be determined, the counter will increase by 1.
myDict = { 'name': 'John', 'age': 18, 'occupation': 'developer', 'habits': ['read books', 'play soccer'] } countVariable = 0 for i in myDict: # Check the data type of Value # If it is integer, the counter is incremented by 1 if isinstance(myDict[i], int): countVariable += 1 # Check the data type of Value # If it is string, the counter is incremented by 1 elif isinstance(myDict[i], str): countVariable += 1 else: countVariable += len(myDict[i]) print("The total length of value is:", countVariable)
Output:
The total length of value is: 5
Summary
If you have any questions about how to get the length of dictionary values in Python, please leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- Return a default value if Key is not in Dictionary in Python
- Sum all values in a dictionary in Python
- Mapping over dictionary values in Python
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java