• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Length of Dictionary Python – Get Dictionary Length with len() Function

Length of Dictionary Python – Get Dictionary Length with len() Function

February 4, 2022 Leave a Comment

To find the length of a dictionary in Python, the easiest way is to use the Python len() function. The len() function returns the number of key/value pairs in the dictionary and the size of the dictionary.

length_of_dictionary = len(dictionary)

In Python, dictionaries are a collection of key/value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily calculate the length and size of a dictionary.

To get the length of a dictionary in Python, we can use the Python len() function.

Let’s say we have the following dictionary in our Python code.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

To get the size of our dictionary variable, we can call the len() function.

dictionary = { "key1":"This", "key2":"is", "key3":"a", "key4":"dictionary." }

length_of_dictionary = len(dictionary)

print(length_of_dictionary)

#Output:
4

As you can see above, there are 4 key/value pairs, and as a result, the len() function returns 4.

Using the Python len() Function to Find the Length of Other Objects in Python

The Python len() function is one of the most used functions in the Python programming language.

You can also use the len() function to find the length of a string, the length of a list, the length of a set, and the length of a tuple in Python.

For example, if I wanted to find the length of the following string variable, I’d just pass it to the len() function.

print(len("This is a string variable."))

#Output:
26

Hopefully this article has been useful for you to learn how to use the Python len() function to find the length of a dictionary.

Other Articles You'll Also Like:

  • 1.  Python acos – Find Arccosine and Inverse Cosine of Number
  • 2.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 3.  How to Read XLSX File from Remote Server Using Paramiko FTP and Pandas
  • 4.  How to Slice a Dictionary in Python
  • 5.  Convert First Letter of String to Lowercase in Python
  • 6.  Using Python to Count Number of Lines in String
  • 7.  Using Selenium to Get Text from Element in Python
  • 8.  Remove None From List Using Python
  • 9.  Get Difference Between datetime Variables in Python
  • 10.  Using Python to Split String into Dictionary

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy