{"id":329323,"date":"2023-10-03T18:42:10","date_gmt":"2023-10-03T18:42:10","guid":{"rendered":"https:\/\/pythonguides.com\/?p=45567"},"modified":"2023-10-03T18:42:10","modified_gmt":"2023-10-03T18:42:10","slug":"check-if-python-dictionary-is-empty-4-methods","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/10\/03\/check-if-python-dictionary-is-empty-4-methods\/","title":{"rendered":"Check if Python Dictionary is Empty (4 methods)"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p>In this article, we will see how to <strong>check if Python dictionary is empty<\/strong> using different methods with some illustrative examples. <\/p>\n<p>In Python, a <a href=\"https:\/\/pythonguides.com\/create-a-dictionary-in-python\/\"  data-type=\"link\" data-id=\"https:\/\/pythonguides.com\/create-a-dictionary-in-python\/\" rel=\"noreferrer noopener\">dictionary<\/a> is a built-in data type that can be used to store a mutable unordered collection of items. Each item stored in a dictionary has a key-value pair. Often, while writing code, it&#8217;s crucial to create an empty dictionary.<\/p>\n<p>An empty dictionary can be created using <strong>curly braces{}<\/strong>, <strong>dict() constructor<\/strong>, or <strong>dictionary comprehension<\/strong> in Python. Also, sometimes we need to check if Python dictionary is empty before performing operations on it.<\/p>\n<h2 class=\"wp-block-heading\">Methods to check if Python Dictionary is Empty<\/h2>\n<p>There are several methods to check if Python dictionary is empty.<\/p>\n<ul>\n<li>By using if not operator<\/li>\n<li>By using bool(dict) method<\/li>\n<li>By using len() method<\/li>\n<li>By using == operator<\/li>\n<\/ul>\n<p>Let&#8217;s see them one by one using some demonstrative examples:<\/p>\n<h3 class=\"wp-block-heading\">Method 1: Check if a dictionary is empty in Python using if not operator<\/h3>\n<p>In Python, you can check if a dictionary is empty using the<strong> if not operator<\/strong>. The <strong>if not operator <\/strong>reviews the boolean value of the object following it, and an empty dictionary evaluates to False in a boolean context. Therefore, using the <strong>if not<\/strong> with a dictionary will execute the block of code following it check if Python dictionary is empty.<\/p>\n<p><strong>Example:<\/strong> Let&#8217;s consider a scenario where we have a dictionary that holds the population of different states. And, we want to check if there is any data in the population dictionary or not.<\/p>\n<p>Imagine you&#8217;re writing a program to analyze US state populations, but you only want it to execute if there&#8217;s data in the dictionary.<\/p>\n<pre class=\"wp-block-code\"><code>state_population = {}\nif not state_population:\n    print(\"No data available.\")\nelse:\n    print(f\"Processing data: {state_population}\")<\/code><\/pre>\n<p><strong>Output: <\/strong>In this case, the output is &#8220;<strong>No data available.<\/strong>&#8221; because the <strong>state_population<\/strong> dictionary is empty.<\/p>\n<pre class=\"wp-block-code\"><code>No data available.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"597\" height=\"381\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-empty-dictionary.jpg\" alt=\"Python empty dictionary\" class=\"wp-image-45573\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-empty-dictionary.jpg 597w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-empty-dictionary-300x191.jpg 300w\" sizes=\"(max-width: 597px) 100vw, 597px\" title=\"Python empty dictionary\"><\/figure>\n<\/div>\n<p>This way we can use the if not operator to check if the dictionary is empty in Python or not.<\/p>\n<h3 class=\"wp-block-heading\">Method 2: Use bool() function in Python to check if the dictionary is empty.<\/h3>\n<p>In Python, the <strong>bool() function<\/strong> is used to return the boolean value of a specified object. The <strong>bool() function<\/strong> returns <strong>True <\/strong>if the specified object has some content, and <strong>False<\/strong> if the object is empty.<\/p>\n<p>When it comes to dictionaries, you can use the <strong>bool() function<\/strong> to check whether a Python dictionary is empty or not. An empty Python dictionary evaluates to <strong>False<\/strong> in a boolean context, while a non-empty dictionary evaluates to <strong>True<\/strong>.<\/p>\n<p><strong>For instance:<\/strong> Let&#8217;s say we are working with a system that keeps track of stock quantities for a bookstore. You want to check if a particular book is in stock or not.<\/p>\n<p>The system returns a <strong>Python dictionary<\/strong> that represents the inventory for a specific book. If the dictionary is empty, it means the book is not in stock. If it has entries, the book is in stock, and the dictionary provides more details about it.<\/p>\n<pre class=\"wp-block-code\"><code>def check_book_stock(book_details):\n    if not bool(book_details):\n        return \"The book is not in stock.\"\n    else:\n        return f\"The book is in stock.\\nDetails: {book_details}\"\n\nbook_inventory_1 = {}\nprint(check_book_stock(book_inventory_1))\n\nbook_inventory_2 = {\n    'title': 'To Kill a Mockingbird',\n    'author': 'Harper Lee',\n    'price': '$10.99',\n    'quantity': 5\n}\nprint(check_book_stock(book_inventory_2))<\/code><\/pre>\n<p><strong>Output:<\/strong> In the first scenario (<strong>book_inventory_1<\/strong>), the dictionary is empty. Using the <strong>bool() function<\/strong>, it evaluates to <strong>False<\/strong>, implying that the book is not in stock.<\/p>\n<p>In the second scenario (<strong>book_inventory_2<\/strong>), the dictionary contains details about the book, indicating that it&#8217;s in stock. When passed to the <strong>bool() function<\/strong>, the dictionary evaluates to <strong>True<\/strong>, and the function returns the book&#8217;s details. The output displays the book&#8217;s details, including its title, author, price, and quantity available in stock.<\/p>\n<pre class=\"wp-block-code\"><code>The book is not in stock.\nThe book is in stock.\nDetails: {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'price': '$10.99', 'quantity': 5}<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-Python-Dictionary-is-Empty-1024x695.jpg\" alt=\"check if Python Dictionary is Empty\" class=\"wp-image-45574\" style=\"width:703px;height:477px\" width=\"703\" height=\"477\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-Python-Dictionary-is-Empty-1024x695.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-Python-Dictionary-is-Empty-300x204.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-Python-Dictionary-is-Empty-768x521.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-Python-Dictionary-is-Empty.jpg 1186w\" sizes=\"(max-width: 703px) 100vw, 703px\" title=\"check if Python Dictionary is Empty\"><\/figure>\n<\/div>\n<p>This way we can use the bool() function in Python to check if dictionary is empty.<\/p>\n<h3 class=\"wp-block-heading\">Method 3: Dictionary is empty in Python check using len() function.<\/h3>\n<p>In Python, we can use the <strong>len() function<\/strong> to check if a dictionary is empty. The <strong>len() function<\/strong> returns the <strong>number of items<\/strong> (key-value pairs) in the dictionary. If the length of the dictionary is <strong>0<\/strong>, then it means the dictionary is empty.<\/p>\n<p><strong>For example:<\/strong> Imagine you&#8217;re working on a system that manages voter registration data for a small county in the USA. You have a Python dictionary where the key is a voter&#8217;s name and the value is their registered address.<\/p>\n<p>However, as you&#8217;re testing the system, you want to check whether there are any voters registered or if the Python dictionary is empty.<\/p>\n<pre class=\"wp-block-code\"><code>voter_data = {}\nif len(voter_data) == 0:\n    print(\"No voters have been registered yet.\")\nelse:\n    print(f\"There are {len(voter_data)} registered voters.\")<\/code><\/pre>\n<p><strong>Output:<\/strong> The Python dictionary <strong>voter_data<\/strong> is empty. The <strong>len() function<\/strong> returns <strong>0<\/strong>, indicating the dictionary has no items. Therefore, the output correctly states that no voters have been registered yet.<\/p>\n<pre class=\"wp-block-code\"><code>No voters have been registered yet.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"383\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-dict-is-empty.jpg\" alt=\"Python dict is empty\" class=\"wp-image-45575\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-dict-is-empty.jpg 716w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-dict-is-empty-300x160.jpg 300w\" sizes=\"(max-width: 716px) 100vw, 716px\" title=\"Python dict is empty\"><\/figure>\n<\/div>\n<p>This is how we can use len() to check if a dictionary is empty in Python.<\/p>\n<h3 class=\"wp-block-heading\">Method 4: Check if a dictionary is empty in Python using the == operator.<\/h3>\n<p>In Python, we can check if a dictionary is empty (i.e., it contains no keys or values) by comparing it with an empty dictionary ({}) using the <strong>== operator<\/strong>. The <strong>== operator<\/strong> is used to compare the values of two objects and returns <strong>True<\/strong> if they are equal, and <strong>False<\/strong> otherwise.<\/p>\n<p><strong>For instance:<\/strong> Let&#8217;s consider a scenario, where we want to check whether a state has any registered vehicles. Let&#8217;s assume the vehicle registrations are stored in a <strong>dictionary in Python<\/strong>, where the keys are license plate numbers and the values are details about the vehicles. If the dictionary Python is empty, that means the state has no registered vehicles.<\/p>\n<pre class=\"wp-block-code\"><code>new_state_vehicle_registrations = {}\nif new_state_vehicle_registrations == {}:\n    print(\"The new state has no registered vehicles.\")\nelse:\n    print(f\"The new state has {len(new_state_vehicle_registrations)} registered vehicles.\")<\/code><\/pre>\n<p><strong>Output:<\/strong> In this case, the <strong>new_state_vehicle_registrations<\/strong> dictionary in Python is empty. This way, by comparing the dictionary with <strong>{}<\/strong>, you can easily check if it is empty or not, and perform actions based on that.<\/p>\n<pre class=\"wp-block-code\"><code>The new state has no registered vehicles.<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"372\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-dict-in-Python-is-empty-1024x372.jpg\" alt=\"check if dict in Python is empty\" class=\"wp-image-45576\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-dict-in-Python-is-empty-1024x372.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-dict-in-Python-is-empty-300x109.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-dict-in-Python-is-empty-768x279.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/check-if-dict-in-Python-is-empty.jpg 1054w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"check if dict in Python is empty\"><\/figure>\n<\/div>\n<p>This way we can use the <strong>== operator<\/strong> to check if the Python dictionary is empty.<\/p>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p>This tutorial explains how to <strong>check if Python dictionary is empty<\/strong> using four different methods such as the <strong>if not operator<\/strong>, the <strong>bool() function<\/strong>, the <strong>len() function<\/strong>, and the <strong>== operator<\/strong> with demonstrative examples. The choice depends on the specific use case and the programmer&#8217;s preference. Whether it&#8217;s through direct Boolean checks or employing built-in functions, Python offers flexibility and efficiency in handling such tasks.<\/p>\n<p>You may also like to read:<\/p>\n<ul>\n<li><a href=\"https:\/\/pythonguides.com\/python-dictionary-multiple-keys\/\"  rel=\"noreferrer noopener\">Python dictionary multiple keys<\/a><\/li>\n<li><a href=\"https:\/\/pythonguides.com\/python-dictionary-comprehension\/\"  rel=\"noreferrer noopener\">Python dictionary comprehension<\/a><\/li>\n<li><a href=\"https:\/\/pythonguides.com\/python-dictionary-filter\/\"  rel=\"noreferrer noopener\">Filter Dictionary Python<\/a><\/li>\n<li><a href=\"https:\/\/google.github.io\/styleguide\/pyguide.html\"  data-type=\"link\" data-id=\"https:\/\/google.github.io\/styleguide\/pyguide.html\" rel=\"noreferrer noopener\">Google Python Style Guide<\/a><\/li>\n<\/ul>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this article, we will see how to check if Python dictionary is empty using different methods with some illustrative examples. In Python, a dictionary is a built-in data type that can be used to store a mutable unordered collection of items. Each item stored in a dictionary has a key-value pair. Often, while writing &#8230; <a title=\"Check if Python Dictionary is Empty (4 methods)\" class=\"read-more\" href=\"https:\/\/pythonguides.com\/check-if-python-dictionary-is-empty-4-methods\/\" aria-label=\"More on Check if Python Dictionary is Empty (4 methods)\">Read more&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":2034,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Check if Python Dictionary is Empty (4 methods) - ITTeacherITFreelance.hk","description":"In this article, we will see how to check if Python dictionary is empty using different methods with some illustrative examples. In Python, a dictionary is a bu"},"footnotes":""},"categories":[10700],"tags":[10769],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329323"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2034"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329323"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329323\/revisions"}],"predecessor-version":[{"id":329324,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329323\/revisions\/329324"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}