{"id":4226,"date":"2022-03-08T14:31:47","date_gmt":"2022-03-08T14:31:47","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4226"},"modified":"2023-11-24T12:03:49","modified_gmt":"2023-11-24T12:03:49","slug":"list-comprehension","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/memory-python\/lists-python\/list-comprehension","title":{"rendered":"List comprehension"},"content":{"rendered":"\n<p>Combines a for loop into the creation of a new list based on an existing list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new_list = &#91;&lt;expression&gt; for &lt;element&gt; in &lt;collection&gt;]\n\n\nsource = &#91;1, 2, 3, 4]\nNewList = &#91;item * 2 for item in source]\nprint(NewList)    \/\/Will print: &#91;2, 4, 6, 8]\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Adding conditional arguments<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">if<\/h5>\n\n\n\n<p>You simply add an if statement to the end:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new_list = &#91;&lt;expression&gt; for &lt;element&gt; in &lt;collection&gt; if &lt;expression&gt;]\n\n\nsource = &#91;1, 2, 3, 4]\nNewList = &#91;item * 2 for item in source if item &gt;= 2]\nprint(NewList)    \/\/Will print: &#91;6, 8]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">if else<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>new_list = &#91;&lt;expression&gt; for &lt;element&gt; in &lt;collection&gt; if &lt;expression&gt;]\n\n\nsource = &#91;1, 2, 3, 4]\nNewList = &#91;item * 2 if item &gt;= 2 else item * 1 for item in source]\nprint(NewList)    \/\/Will print: &#91;1, 2, 6, 8]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Examples<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Create list with 100 entries of 0<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;0 for i in range(5)]    #Create a list 5 entries long containing 0<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Create list with 100 random entries<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>random_list = &#91;random.randint(0, 100) for i in range(5)]    #Create a list 5 entries long that has a random number in each from 0 to 100 (inclusive)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Combines a for loop into the creation of a new list based on an existing list. Adding conditional arguments if You simply add an if statement to the end: if else Examples Create list with 100 entries of 0 Create list with 100 random entries<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325,328],"tags":[],"class_list":["post-4226","post","type-post","status-publish","format-standard","hentry","category-lists-python","category-loops-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4226"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4226\/revisions"}],"predecessor-version":[{"id":4394,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4226\/revisions\/4394"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}