{"id":2090,"date":"2019-06-02T08:05:13","date_gmt":"2019-06-02T06:05:13","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2090"},"modified":"2019-06-02T21:20:38","modified_gmt":"2019-06-02T19:20:38","slug":"python-3-8-new-feature-the-walrus-operator","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-3-8-new-feature-the-walrus-operator\/","title":{"rendered":"Python 3.8 new feature: the Walrus operator"},"content":{"rendered":"<p>You can find the new features (post) here: <a href=\"https:\/\/www.codementor.io\/ruturajkiranvaidya\/introduction-to-python-3-8-new-feature-the-walrus-operator-vcv2d3zxw?utm_swu=7179\">https:\/\/www.codementor.io\/ruturajkiranvaidya\/introduction-to-python-3-8-new-feature-the-walrus-operator-vcv2d3zxw?utm_swu=7179<\/a><\/p>\n<h2>The walrus operator :=<\/h2>\n<p>This operator, the Walrus operator, let you assigns the value of a variable to another to use it as part of an expression.<\/p>\n<pre class=\"lang:default decode:true\">sample_data = [\r\n    {\"userId\": 1, \"id\": 1, \"title\": \"delectus aut autem\", \"completed\": False},\r\n    {\"userId\": 1, \"id\": 2, \"title\": \"quis ut nam facilis\", \"completed\": False},\r\n    {\"userId\": 1, \"id\": 3, \"title\": \"fugiat veniam minus\", \"completed\": False},\r\n    {\"userId\": 1, \"id\": 4, \"title\": \"et porro tempora\", \"completed\": True},\r\n    {\"userId\": 1, \"id\": 4, \"title\": None, \"completed\": True},\r\n]\r\n\r\nprint(\"With Python 3.8 Walrus Operator:\") \r\nfor entry in sample_data: \r\n    if title := entry.get(\"title\"):\r\n        print(f'Found title: \"{title}\"')\r\n\r\nprint(\"Without Walrus operator:\")\r\nfor entry in sample_data:\r\n    title = entry.get(\"title\")\r\n    if title:\r\n        print(f'Found title: \"{title}\"')<\/pre>\n<p>Example from (https:\/\/medium.com\/hultner\/try-out-walrus-operator-in-python-3-8-d030ce0ce601).<\/p>\n<pre class=\"lang:default decode:true\">ith Python 3.8 Walrus Operator:\r\nFound title: \"delectus aut autem\"\r\nFound title: \"quis ut nam facilis\"\r\nFound title: \"fugiat veniam minus\"\r\nFound title: \"et porro tempora\"\r\nWithout Walrus operator:\r\nFound title: \"delectus aut autem\"\r\nFound title: \"quis ut nam facilis\"\r\nFound title: \"fugiat veniam minus\"\r\nFound title: \"et porro tempora\"<\/pre>\n<h2>Other example<\/h2>\n<pre class=\"lang:default decode:true \">import re\r\n\r\nwith open(\"sample.txt\") as file:\r\n    for line in file:\r\n        if text := re.findall(\"f\",line):\r\n            print(line.strip())\r\n            print(text)\r\n\r\nprint(\"\\n--------- without := -------------\\n\")\r\n\r\nwith open(\"sample.txt\") as file:\r\n    for line in file:\r\n        text = re.findall(\"f\",line)\r\n        if (text):\r\n            print(line.strip())\r\n            print(text)<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">fafsdfg\r\n['f', 'f', 'f']\r\nsdfgs\r\n['f']\r\nfdg\r\n['f']\r\nhsfhfghfs\r\n['f', 'f', 'f']\r\n\r\n--------- without := -------------\r\n\r\nfafsdfg\r\n['f', 'f', 'f']\r\nsdfgs\r\n['f']\r\nfdg\r\n['f']\r\nhsfhfghfs\r\n['f', 'f', 'f']<\/pre>\n<p>&nbsp;<\/p>\n<h2>Other &#8230; examples<\/h2>\n<pre class=\"lang:default decode:true\"># instead of\r\n\r\nx = [f(x), f(x)**2, f(x)**3]\r\n\r\n# you can do\r\n\r\nx = [y := f(x), y**2, y**3]\r\n\r\n# for more efficiency<\/pre>\n<pre class=\"lang:default decode:true  \">lines = [1,2,3]\r\n\r\ndef f(x):\r\n    if x &gt; 1:\r\n        return x\r\n\r\nd = []\r\nfor l in lines:\r\n    if f(l):\r\n        d.append(l)\r\n\r\n# you can do so\r\nd = [y for l in lines if (y := f(l))]<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"The new Walrus operator\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-3-8-new-feature-the-walrus-operator\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2095,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[404],"tags":[403,402],"class_list":["post-2090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-8","tag-python-3-8","tag-walrus-operator"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2090"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2090\/revisions"}],"predecessor-version":[{"id":2097,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2090\/revisions\/2097"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2095"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}