{"id":2140,"date":"2019-04-21T14:03:00","date_gmt":"2019-04-21T21:03:00","guid":{"rendered":"https:\/\/www.stevenbrown.ca\/blog\/?p=2140"},"modified":"2019-04-22T18:57:55","modified_gmt":"2019-04-23T01:57:55","slug":"python-3-iterable-unpacking-catch-all","status":"publish","type":"post","link":"https:\/\/www.stevenbrown.ca\/blog\/archives\/2140","title":{"rendered":"Python 3 Iterable Unpacking Catch-all"},"content":{"rendered":"<p> I&#8217;m one of those terrible people that <i>still<\/i> haven&#8217;t switched over to using <i>Python3<\/i> &#x2026;just yet.  But one thing that pleasantly surprised me recently was <a href=\"https:\/\/www.youtube.com\/watch?v=C-gEQdGVXbk&amp;list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&amp;index=104&amp;t=782s\">learning<\/a> of the &#8220;new&#8221; <i>starred expression<\/i> catch-all that can be used when unpacking iterables.  This lets you use up to one starred expression to receive <i>the rest<\/i> of the items of an iterable during an unpacking assignment. <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfull_name = &quot;Steven W J Brown&quot;\n\n#python2\nnames             = full_name.split()                # &#x5B;&#039;Steven&#039;, &#039;W&#039;, &#039;J&#039;, &#039;Brown&#039;]\nfirst, last       = names&#x5B;0], names&#x5B;-1]              # 2.A: ignore middle names\nfirst, mids, last = names&#x5B;0], names&#x5B;1:-1], names&#x5B;-1] # 2.B: store middle names\n\n#python3, \nfirst, *_, last    = full_name.split() # 3.A: ignore middle names\nfirst, *mids, last = full_name.split() # 3.B: store middle names\n<\/pre>\n<p> It&#8217;s easy to read, eliminates the need for an intermediary variable and makes the code cleaner.  And it can be used in loops.  More details here: <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-3132\/\">PEP 3132<\/a>.  Love it. <\/p>\n<p> I really have to start re-configuring my environments for python3&#x2026;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m one of those terrible people that still haven&#8217;t switched over to using Python3 &#x2026;just yet. But one thing that pleasantly surprised me recently was learning of the &#8220;new&#8221; starred expression catch-all that can be used when unpacking iterables. This lets you use up to one starred expression to receive the rest of the items&hellip; <a class=\"more-link\" href=\"https:\/\/www.stevenbrown.ca\/blog\/archives\/2140\">Continue reading <span class=\"screen-reader-text\">Python 3 Iterable Unpacking Catch-all<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[46],"class_list":["post-2140","post","type-post","status-publish","format-standard","hentry","category-geek","tag-python","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4jEMb-yw","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/2140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/comments?post=2140"}],"version-history":[{"count":4,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/2140\/revisions"}],"predecessor-version":[{"id":2156,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/posts\/2140\/revisions\/2156"}],"wp:attachment":[{"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/media?parent=2140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/categories?post=2140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stevenbrown.ca\/blog\/wp-json\/wp\/v2\/tags?post=2140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}