HomeLinuxSize of Dictionary Python

Size of Dictionary Python


Python Dictionaries are an effective way of getting your Knowledge within the type of key-value pairs, similar to the JSON. Nevertheless, changing into programmer requires you to be good within the optimization of your code and its working. To do that, step one is to turn out to be conscious of the scale of the weather that you’re utilizing and the reminiscence utilization/storage they’re consuming.

This submit will act as your information to discovering the size of Python Dictionaries and can comprise the next sections:

Methodology 1: Use the len() Methodology to Discover the Size of Dictionary

The len() is a built-in technique that’s used to counter the size of varied components in Python. When this technique is used to seek out the size of a Python Dictionary, it really works as a counter and returns the entire variety of key-value pairs inside that Dictionary.

To exhibit the usage of the len() technique within the case of Python Dictionary, take the next code:

dictVar = {“title”: “John Doe”, “Age”:20, “Social”: “LinuxHint”}
print(len(dictVar))

 

When this code is executed, it can present the size of the dictionary as:

As you possibly can see, the len() technique returns the variety of key-value pairs within the dictionary, which is “3”.

Methodology 2: Use the len() Methodology to get the Size of a Nested Dictionary

In case you are attempting to use the len() technique on a nested record, then you’ll solely get the variety of non-nested key-value pairs. For instance, take the next code:

dictVar={‘individuals’:
         {‘John’: ‘Doe’, ‘Johny’: ‘Wick’, ‘Rudy’: ‘Smith’},
       ‘Meals’:
         {‘Quick’: ‘Burger’, ‘FastFood’: ‘Ham’, ‘Steak’: ‘Medium’}}
print(len(dictVar))

 

Operating this code will print the next consequence:

This reply is neither proper nor fallacious. In case you speak about this “dictVar,” then it has two components. Nevertheless, if you happen to have been to speak about its nested components, then it has extra components than 2. In such circumstances, it is advisable apply the len() technique on every father or mother ingredient and sum the consequence:

dictVar={‘individuals’:
         {‘John’: ‘Doe’, ‘Johny’: ‘Wick’, ‘Rudy’: ‘Smith’},
       ‘Meals’:
         {‘Quick’: ‘Burger’, ‘FastFood’: ‘Ham’, ‘Steak’: ‘Medium’}}
print(len(dictVar[‘people’])+len(dictVar[‘Food’]))

 

This time round, when this program is executed, it can show the variety of nested key-value pairs as:

As you possibly can see, the variety of nested key-value pairs is 6.

Nevertheless, if you wish to discover the whole size of the dictionary, which means the size of the father or mother dictionary and the nested dictionary, then merely use len() on the father or mother dictionary and add it:

dictVar={‘individuals’:
         {‘John’: ‘Doe’, ‘Johny’: ‘Wick’, ‘Rudy’: ‘Smith’},
       ‘Meals’:
         {‘Quick’: ‘Burger’, ‘FastFood’: ‘Ham’, ‘Steak’: ‘Medium’}}
print(len(dictVar)+len(dictVar[‘people’])+len(dictVar[‘Food’]))

 

With this, the output is displayed as:

This output implies that there are a complete of 8 key-value pairs, nested and non-nested.

Methodology 3: Use the isinstance() Methodology to Get Complete Size of Nested Dictionary

To determine the whole size of a dictionary, you may as well make the most of the isinstance() technique together with the len() technique. The isinstance() technique checks whether or not the worth within the first argument exists within the subclass.

To exhibit the working of the isinstance() technique to seek out the size of the dictionary, take the next code:

dictVar={‘individuals’:
         {‘John’: ‘Doe’, ‘Johny’: ‘Wick’, ‘Rudy’: ‘Smith’},
       ‘Meals’:
         {‘Quick’: ‘Burger’, ‘FastFood’: ‘Ham’, ‘Steak’: ‘Medium’}}

lengthVar = len(dictVar)
for a in dictVar.values():
    if isinstance(a, dict):
      lengthVar += len(a)
print(“size of the dictionary is”, lengthVar)

 

When this code is executed, it can produce the next consequence on the terminal:

The size of the dictionary is “8”.

Methodology 4: Use getsizeof() Methodology to Get the Reminiscence Consumed by Dict

If you wish to determine the reminiscence consumed by the dictionary in bytes, then you possibly can make the most of the getsizeof() technique. Nevertheless, to make use of this technique it is advisable import the “sys” module.

To exhibit the working of the getsizeof() technique, use the next code snippet:

import sys
dictVar={‘individuals’:
         {‘John’: ‘Doe’, ‘Johny’: ‘Wick’, ‘Rudy’: ‘Smith’},
       ‘Meals’:
         {‘Quick’: ‘Burger’, ‘FastFood’: ‘Ham’, ‘Steak’: ‘Medium’}}

print(sys.getsizeof(dictVar))

 

It will print the next consequence on the terminal:

In response to the output, the reminiscence consumed by the dictionary “dictVar” is 184 bytes.

Conclusion

To search out the size of a Dictionary variable in Python, the consumer could make use of the len() technique, which is a built-in technique. This technique can even work for nested Dictionaries. Nevertheless, within the case of nested Dictionaries, it is not going to return the depend of key-value pairs of the nested dict however solely of the father or mother dict. To get the reminiscence consumption of the dictionary the consumer can make the most of the getsizeof() technique from the “sys” package deal.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments