{"id":2041,"date":"2019-08-22T20:27:57","date_gmt":"2019-08-22T14:57:57","guid":{"rendered":"http:\/\/www.mka.in\/wp\/?p=2041"},"modified":"2019-08-27T09:52:30","modified_gmt":"2019-08-27T04:22:30","slug":"python-filter-function","status":"publish","type":"post","link":"https:\/\/www.mka.in\/wp\/python-filter-function\/","title":{"rendered":"Python filter function"},"content":{"rendered":"\n<p>This is an interesting feature in python. Suppose you have a list of items and you want to apply quick filters such as &#8220;greater than x&#8221;, &#8220;less than x&#8221;, &#8220;above average&#8221;, &#8220;below average&#8221; on list items, then filter() method makes it quite handy.<\/p>\n\n\n\n<p>Typical syntax of filter function is like filter(some_func_name, list_of_items)<\/p>\n\n\n\n<p>You can also think of applying this method to sort on the basis of size, color, shape or any other attribute of object.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#!\/usr\/bin\/python3\n\nimport statistics\n\n# Score of students\nscores = [34, 65, 87, 23 , 67, 90, 54, 12, 72, 50, 29]\n\nprint(\"average score is\", statistics.mean(scores))\n\n# filter above average scores\nabove_avg = list(filter(lambda x: x > statistics.mean(scores), scores))\nprint(\"above average scores\", above_avg)\n\n# filter below average scores\nbelow_avg = list(filter(lambda x: x &lt; statistics.mean(scores), scores))\nprint(\"below average scores\", below_avg)\n\n#  filter scores less than passing marks, say 40\nnot_pass = list(filter(lambda x: x &lt; 40, scores))\nprint(\"below pass marks\", not_pass)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n.\/filter.py \naverage score is 53\nabove average scores [65, 87, 67, 90, 54, 72]\nbelow average scores [34, 23, 12, 50, 29]\nbelow pass marks [34, 23, 12, 29]<\/code><\/pre>\n\n\n\n<p>Practically, you will more often use filters on dictionary objects for example knowing student names who have score above\/below average and students who have passed or failed. Here is an example of this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#!\/usr\/bin\/python3\n\nimport statistics\n\n# Score of students\n\nstudents = { 'Dexter' : 34, 'Popoye' : 65, 'Baloo' : 87, 'Mogali' : 23, 'Peppa' : 67, 'Tiger' : 90, 'Pumba' : 54, 'Timon' : 12, 'Jerry' : 72, 'Tom' : 50, 'McDuck' : 29 }\n\nprint(\"Average Score\", statistics.mean(list(students.values())))\n\n# filter above average scores\nabove_avg = dict(filter(lambda x: x[1] > statistics.mean(list(students.values())), students.items()))\nprint(\"Above average scores\", above_avg)\n\n# filter below average scores\nbelow_avg = dict(filter(lambda x: x[1] &lt; statistics.mean(list(students.values())), students.items()))\nprint(\"Below average scores\", below_avg)\n\n#  filter scores less than passing marks, say 40\nfail_students = dict(filter(lambda x: x[1] &lt; 40, students.items()))\nprint(\"Students scoring less than 40\", fail_students)\n\n#  filter scores less than passing marks, say 40\npass_students = dict(filter(lambda x: x[1] > 40, students.items()))\nprint(\"Students scoring more than 40\", pass_students)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n.\/students.py \nAverage Score 53\nAbove average scores {'Popoye': 65, 'Baloo': 87, 'Peppa': 67, 'Tiger': 90, 'Pumba': 54, 'Jerry': 72}\n\nBelow average scores {'Dexter': 34, 'Mogali': 23, 'Timon': 12, 'Tom': 50, 'McDuck': 29}\n\nStudents scoring less than 40 {'Dexter': 34, 'Mogali': 23, 'Timon': 12, 'McDuck': 29}\n\nStudents scoring more than 40 {'Popoye': 65, 'Baloo': 87, 'Peppa': 67, 'Tiger': 90, 'Pumba': 54, 'Jerry': 72, 'Tom': 50}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is an interesting feature in python. Suppose you have a list of items and you want to apply quick filters such as &#8220;greater than x&#8221;, &#8220;less than x&#8221;, &#8220;above average&#8221;, &#8220;below average&#8221; on list items, then filter() method makes it quite handy. Typical syntax of filter function is like filter(some_func_name, list_of_items) You can also [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1474,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[100,98,96,53],"class_list":["post-2041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech-bytes","tag-filter","tag-function","tag-programming","tag-python"],"_links":{"self":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":9,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2056,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts\/2041\/revisions\/2056"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/media\/1474"}],"wp:attachment":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}