Warning: session_start(): open(/tmp/sess_930295e2d87e0e9f7d293b14861bcc0a, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Get The length Of Dictionary Values In Python - LearnShareIT

How To Get The length Of Dictionary Values In Python

Get the length of Dictionary Values in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *