{"id":391,"date":"2024-03-18T15:33:38","date_gmt":"2024-03-18T15:33:38","guid":{"rendered":"https:\/\/offensivepython.com\/?p=391"},"modified":"2024-03-18T15:39:53","modified_gmt":"2024-03-18T15:39:53","slug":"list-comprehensions","status":"publish","type":"post","link":"https:\/\/offensivepython.com\/index.php\/2024\/03\/18\/list-comprehensions\/","title":{"rendered":"List Comprehensions"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"391\" class=\"elementor elementor-391\">\n\t\t\t\t<div class=\"elementor-element elementor-element-7b82517 e-flex e-con-boxed e-con e-parent\" data-id=\"7b82517\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-71f8aca6 elementor-widget elementor-widget-text-editor\" data-id=\"71f8aca6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<p class=\"wp-block-paragraph\">\u00a0 \u00a0 \u00a0Python has a really cool feature known as comprehensions.\u00a0 Below are a few examples of how this might work with lists.\u00a0 They can also be implemented for dictionaries as well.\u00a0<\/p>\n<p>\u00a0 \u00a0 \u00a0Say we want to create a list of characters that are part of a string containing a phrase.\u00a0 Traditionally, we would approach that problem like this:<\/p>\n<div>\n<p><strong>characters = []<\/strong><br \/><strong>phrase = &#8220;Python 3!&#8221;<\/strong><br \/><strong>for character in phrase:<\/strong><br \/><strong>\u00a0 \u00a0 characters.append(character)<\/strong><br \/><strong>print(characters)<\/strong><\/p>\n<p>But with list comprehension, it can be reduced to this:<\/p>\n<div>\n<p><strong>characters = [character for character in phrase]<\/strong><br \/><strong>print(characters)<\/strong><\/p>\n<\/div>\n<p>Output is the same for both:<\/p>\n<p>[&#8216;P&#8217;, &#8216;y&#8217;, &#8216;t&#8217;, &#8216;h&#8217;, &#8216;o&#8217;, &#8216;n&#8217;, &#8216; &#8216;, &#8216;3&#8217;, &#8216;!&#8217;]<br \/>[&#8216;P&#8217;, &#8216;y&#8217;, &#8216;t&#8217;, &#8216;h&#8217;, &#8216;o&#8217;, &#8216;n&#8217;, &#8216; &#8216;, &#8216;3&#8217;, &#8216;!&#8217;]<\/p>\n<p>\u00a0 \u00a0 \u00a0To help remember how to implement a list comprehension, always think &#8220;[new_item for item in list]&#8221;.\u00a0 But we can also have a condition with a list comprehension.\u00a0 Lets say we want to convert names to all upper case, but only if they have more than 4 characters in them.\u00a0 Below is how to accomplish that with a list comprehension.<\/p>\n<div>\n<p><strong>names = [&#8220;Bill&#8221;, &#8220;Maria&#8221;, &#8220;Christopher&#8221;, &#8220;Sam&#8221;]<\/strong><br \/><strong>new_names = [name.upper() for name in names if len(name) &gt; 4]<\/strong><br \/><strong>print(new_names)<\/strong><\/p>\n<p>Output:<\/p>\n<p>[&#8216;MARIA&#8217;, &#8216;CHRISTOPHER&#8217;]<\/p>\n<\/div>\n<p>\u00a0<\/p>\n<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0Python has a really cool feature known as comprehensions.\u00a0 Below are a few examples of how this might work with lists.\u00a0 They can also be implemented for dictionaries as well.\u00a0 \u00a0 \u00a0 \u00a0Say we want to create a list of characters that are part of a string containing a phrase.\u00a0 Traditionally, we would [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[27],"tags":[18],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-compound-data-types","tag-list-comprehension"],"_links":{"self":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":7,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/391\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}