{"id":514,"date":"2024-10-19T03:27:08","date_gmt":"2024-10-19T03:27:08","guid":{"rendered":"https:\/\/itsmycode.com\/?p=514"},"modified":"2024-10-19T03:27:10","modified_gmt":"2024-10-19T03:27:10","slug":"python-any","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-any\/","title":{"rendered":"Python any()"},"content":{"rendered":"\n<p>The <strong><code>any()<\/code><\/strong> function in Python returns <strong><code>True <\/code><\/strong>if any element of an iterable(<strong>List<\/strong>, <strong>set<\/strong>, <strong>dictionary<\/strong>, <strong>tuple<\/strong>) is True. If not, it returns <strong><code>False<\/code><\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"any-syntax\">any() Syntax<\/h2>\n\n\n\n<p>The syntax of <strong><code>any()<\/code><\/strong> method is&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>any(iterable)<\/strong><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"any-parameters\">any() Parameters<\/h2>\n\n\n\n<p>The <strong><code>any()<\/code><\/strong> function takes iterable as an argument the iterable can be of type&nbsp;<strong>list<\/strong>,&nbsp;<strong>set<\/strong>,&nbsp;<strong>tuple<\/strong>,&nbsp;<strong>dictionary<\/strong>, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"any-return-value\">any() Return Value<\/h2>\n\n\n\n<p>The <strong><code>any()<\/code><\/strong> method returns a boolean value.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>True <\/code><\/strong>if one of the elements in iterable is true<\/li>\n\n\n\n<li><strong><code>False <\/code><\/strong>if all the elements in iterable are false or if the iterable is empty<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Condition<\/th><th>Return Value<\/th><\/tr><tr><td>All elements are true<\/td><td>True<\/td><\/tr><tr><td>All elements are false<\/td><td>False<\/td><\/tr><tr><td>One element is true and others are false)<\/td><td>True<\/td><\/tr><tr><td>One element is false and others are true<\/td><td>True<\/td><\/tr><tr><td>Empty Iterable<\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-using-any-function-on-python-lists\">Example 1 \u2013 Using any() function on Python Lists<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># All the elements in the list are true\nlist = &#91;1,3,5,7]\nprint(any(list))\n\n# All the elements in the list are false\nlist = &#91;0,0,False]\nprint(any(list))\n\n# Some of the elements are false\nlist = &#91;1,5,7,False]\nprint(any(list))\n\n\n# Only 1 element is true\nlist = &#91;0, False, 5]\nprint(any(list))\n\n# False since its Empty iterable \nlist = &#91;]\nprint(any(list))<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue\nTrue\nFalse<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-using-any-function-on-python-strings\">Example 2 \u2013 Using any() function on Python Strings<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Non Empty string returns True\nstring = \"Hello World\"\nprint(any(string))\n\n#  0 is False but the string character of 0 is True \nstring = '000'\nprint(any(string))\n\n# False since empty string and not iterable\nstring = ''\nprint(any(string))<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nTrue\nFalse<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-3-using-any-function-on-python-dictionaries\">Example 3 \u2013 Using any() function on Python Dictionaries<\/h2>\n\n\n\n<p>In the case of a dictionary, only if all the keys(<strong>not values<\/strong>) of the dictionary are either false or if the dictionary is empty, the <strong><code>any()<\/code><\/strong> method returns False. If at least one key is true, then <strong><code>any()<\/code><\/strong> returns True.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># All elements in dictionary are true\ndict = {1: 'Hello', 2: 'World'}\nprint(any(dict))\n\n# All elements in dictionary are false\ndict = {0: 'Hello', False: 'World'}\nprint(any(dict))\n\n\n# Some elements in dictionary are true and rest are false\ndict = {0: 'Hello', 1: 'World', False: 'Welcome'}\nprint(any(dict))\n\n# Empty Dictionary returns false\ndict = {}\nprint(any(dict))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue\nFalse<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-4-using-any-function-on-python-tuples\">Example 4 \u2013 Using any() function on Python Tuples<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># All elements of tuple are true\nt = (1, 2, 3, 4)\nprint(any(t))\n\n# All elements of tuple are false\nt = (0, False, False)\nprint(any(t))\n\n# Some elements of tuple are true while others are false\nt = (5, 0, 3, 1, False)\nprint(any(t))\n\n# Empty tuple returns false\nt = ()\nprint(any(t))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue\nFalse<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-5-using-any-function-on-python-sets\">Example 5 \u2013 Using any() function on Python Sets<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># All elements of set are true\ns = {1, 2, 3, 4}\nprint(any(s))\n\n# All elements of set are false\ns = {0, 0, False}\nprint(any(s))\n\n# Some elements of set are true while others are false\ns = {1, 2, 3, 0, False}\nprint(any(s))\n\n# Empty set returns false\ns = {}\nprint(any(s))\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue\nFalse<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The any() function in Python returns True if any element of an iterable(List, set, dictionary, tuple) is True. If not, it returns False. any() Syntax The syntax of any() method is&nbsp; any(iterable) any() Parameters The any() function takes iterable as an argument the iterable can be of type&nbsp;list,&nbsp;set,&nbsp;tuple,&nbsp;dictionary, etc. any() Return Value The any() method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":515,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[],"class_list":["post-514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=514"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/514\/revisions\/516"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/515"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}