{"id":17706,"date":"2021-01-19T05:44:44","date_gmt":"2021-01-19T13:44:44","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=17706"},"modified":"2023-12-01T04:08:12","modified_gmt":"2023-12-01T12:08:12","slug":"python-list-comprehension","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/","title":{"rendered":"Python List Comprehension: A How-To Guide"},"content":{"rendered":"\n<p><em>A Python list comprehension is a way of creating a list based on an existing list. List comprehensions are commonly used to filter items out of a list or to change the values in an existing list. List comprehensions are enclosed within square brackets.<\/em><\/p>\n\n\n\n<p>When you\u2019re working with lists, you may want to create a list based on the contents of an existing sequence. For example, you may want to create a list based on a sequence of characters. Or you may want to a list that multiplies the contents of another list by two.<\/p>\n\n\n\n<p>That\u2019s where list comprehensions come in. This tutorial will explore, with examples, the basics of Python lists, and how to use list comprehensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Lists: A Refresher<\/h2>\n\n\n\n<p>The list data structure allows you to store collections of items in Python. Lists are commonly used when you want to work with multiple values that are related in some way.<\/p>\n\n\n\n<p>For instance, you could use a list to store all the flavors of ice cream sold in an ice cream store. Or you could use a list to store a list of the phone numbers of members at a wine club.<\/p>\n\n\n\n<p>Here\u2019s an example of a list in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>pizzas = ['Chicken', 'Margherita', 'Chicken and Bacon', 'Vegan Special', 'Spinach and Brie', 'BBQ Chicken']<\/pre><\/div>\n\n\n\n<p>Now that we&#8217;ve revised the basics of lists, we can start talking about how to use list comprehensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python List Comprehension<\/h2>\n\n\n\n<p>A Python list comprehension creates a new list from the contents of another list. You can use a list comprehension to duplicate a list or to modify the contents of an existing list into a new list. Or you can transfer the contents of another iterable into a list with a list comprehension.<\/p>\n\n\n\n<p>You can specify filters so that your new list only includes certain values. For instance, you could create a new list from a list of numbers. Your new list may only include numbers greater than 250.<\/p>\n\n\n\n<p>The syntax for a list comprehension in Python is as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[expression for item in list]<\/pre><\/div>\n\n\n\n<p>This syntax is similar to a <a href=\"https:\/\/careerkarma.com\/blog\/python-for-loop\/\">Python <em>for<\/em> statement<\/a>. But, the statement is on one line. To distinguish between a for statement, a list comprehension is enclosed within square brackets.<\/p>\n\n\n\n<p>There are three parts to the syntax above:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>expression<\/strong>: The end value that goes into the new list.<\/li><li><strong>item<\/strong>: The individual item in the list over which you iterate.<\/li><li><strong>list<\/strong>: The list or iterable object through which the list comprehension traverses (acceptable data types include tuples, strings, and lists).<\/li><\/ul>\n\n\n\n<p>You can use a list comprehension with a <a href=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\">Python if&#8230;else statement<\/a> inside:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[expression for item in list if condition else none]<\/pre><\/div>\n\n\n\n<p>This comprehension adds items to the list only if they meet the specified condition.<\/p>\n\n\n\n<p>Using list comprehensions lets you to create a new list from an existing one without defining full <em>for<\/em> statements. <em>for<\/em> statements take up at least two lines of code whereas you can write a list comprehension on one line.<\/p>\n\n\n\n<p>Some people say that list comprehensions are more Pythonic code. This is because they are more efficient than using a short <em>for<\/em> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python List Comprehension Example<\/h2>\n\n\n\n<p>Suppose we want to make a list of all the chicken pizzas we sell at our store. We&#8217;re going to move all chicken pizzas into a <em>Chicken<\/em> category on our menu. To do so, we could filter our pizzas using <em>for<\/em> statements. Or, we could filter our list using a list comprehension.<\/p>\n\n\n\n<p>We could use a list comprehension to generate a new list of pizzas whose names contain <em>Chicken<\/em> based on our existing list of pizzas. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>pizzas = ['Chicken', 'Margherita', 'Chicken and Bacon', 'Vegan Special', 'Spinach and Brie', 'BBQ Chicken']\n\nchicken_pizzas = [p for p in pizzas if &quot;Chicken&quot; in p]\nprint(chicken_pizzas)\n<\/pre><\/div>\n\n\n\n<p>Our comprehension returns the following output list:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[\u2018Chicken\u2019, \u2018Chicken and Bacon\u2019, \u2018BBQ Chicken\u2019]<\/pre><\/div>\n\n\n\n<p>In our code, we first define a list of pizzas on our menu. Our pizzas are stored in the <a href=\"https:\/\/careerkarma.com\/blog\/python-variables\/\">Python variable<\/a> called <em>pizzas<\/em>. Then, we use a list comprehension to create a new list of pizzas whose names contain <em>Chicken.<\/em><\/p>\n\n\n\n<p>Our list comprehension consists of the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>p<\/strong>: This is the value to be added to our list.<\/li><li><strong>for p in pizzas<\/strong>: This loops through every pizza in our \u201cpizzas\u201d list.<\/li><li><strong>if \u201cChicken\u201d in p<\/strong>: This checks to see if each pizza contains \u201cChicken\u201d. If this evaluates to True, the value stored in \u201cp\u201d is added to our list.<\/li><\/ul>\n\n\n\n<p>Our list comprehension only took up one line of code. Whereas in our next example, we need to use three lines of code with a <em>for<\/em> statement to make it work. Our next example shows the extent to which list comprehensions help you write cleaner code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without a List Comprehension<\/h3>\n\n\n\n<p>Let\u2019s return to the chicken pizza example. Without using a list comprehension, if we wanted to generate a list of chicken pizzas on our menu, we would use this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>pizzas = ['Chicken', 'Margherita', 'Chicken and Bacon', 'Vegan Special', 'Spinach and Brie', 'BBQ Chicken']\nchicken_pizzas = []\n\nfor pizza in pizzas:\n\tif &quot;Chicken&quot; in pizza:\n\t\tchicken_pizzas.append(pizza)\n\nprint(chicken_pizzas)\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[\u2018Chicken\u2019, \u2018Chicken and Bacon\u2019, \u2018BBQ Chicken\u2019]<\/pre><\/div>\n\n\n\n<p>First, we define two lists. One list stores a list of pizzas sold at our store. The other list stores a list of all the chicken pizzas we sell. The list of chicken pizzas we sell is initially empty.<\/p>\n\n\n\n<p>We use a \u201cfor\u201d statement to go through each pizza in our list of pizzas. Then, we check to see if each pizza contains the word <em>Chicken.<\/em> If a pizza contains the word <em>Chicken<\/em>, we add that pizza to our list of chicken pizzas. In this case, three pizzas contain <em>Chicken<\/em>, and all three of those pizzas are added to our <em>chicken_pizzas<\/em> list.<\/p>\n\n\n\n<p>As you can see, to check if a pizza contains <em>Chicken<\/em> and to add it to our list, we use three lines of code. There is a more efficient way to write this code: using list comprehensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List Comprehensions Using if\u2026else<\/h2>\n\n\n\n<p>You can also use an <em>if&#8230;else<\/em> statement with a list comprehension.<\/p>\n\n\n\n<p>Earlier, we used an <em>if<\/em> statement to add a pizza to our list of chicken pizzas. We only added a pizza if the name of the pizza included the term <em>Chicken<\/em>. But, suppose we are making a list of which pizzas are vegetarian.<\/p>\n\n\n\n<p>We want to add \u201cMeat\u201d to a list if a pizza name contains <em>Chicken<\/em> and <em>Vegetarian<\/em> if a pizza name does not contain <em>Chicken<\/em>.<\/p>\n\n\n\n<p>This can be done using a list comprehension:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>pizzas = ['Chicken', 'Margherita', 'Chicken and Bacon', 'Vegan Special', 'Spinach and Brie', 'BBQ Chicken']\n\nis_veggie = [&quot;Meat&quot; if &quot;Chicken&quot; in p else &quot;Vegetarian&quot; for p in pizzas]\n\nprint(is_veggie)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[\u2018Meat\u2019, \u2018Vegetarian\u2019, \u2018Meat\u2019, \u2018Vegetarian\u2019, \u2018Vegetarian\u2019, \u2018Meat\u2019]<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down our code. First, we declare a list of pizzas called <em>pizzas<\/em>. Then, we use a list comprehension to assess whether a pizza name contains <em>Chicken<\/em> or not.<\/p>\n\n\n\n<p>If a pizza name contains <em>Chicken<\/em>, the value <em>Meat<\/em> is added to our <em>is_veggie<\/em> list. Otherwise, the value <em>Vegetarian<\/em> is added.<\/p>\n\n\n\n<p>As you can see, the first value in our <em>is_veggie<\/em> list is <em>Meat<\/em>, because its corresponding value in the <em>pizzas<\/em> list is <em>Chicken<\/em>. But, our next value is <em>Vegetarian<\/em>, because its corresponding value in the <em>pizzas<\/em> list is <em>Margherita<\/em>, which does not contain <em>Chicken<\/em>.<\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@careerkarma\/Python-List-Comprehension?lite=true\" width=\"100%\" height=\"400px\" frameborder=\"0\"><\/iframe>\n<br>\n<br>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>List comprehensions allow you to create a new list based on an existing list. Using list comprehensions, you can create a copy of a list, or create a new list that filters out values from an old list.<\/p>\n\n\n\n<p>This tutorial discussed, with reference to examples, the basics of Python lists and how to use the list comprehension technique to create new lists.<\/p>\n\n\n\n<p>Do you want to learn more about coding in Python? Check out our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>. This guide contains a list of learning resources to help you continue your journey toward mastering Python.<\/p>\n","protected":false},"excerpt":{"rendered":"A Python list comprehension is a way of creating a list based on an existing list. List comprehensions are commonly used to filter items out of a list or to change the values in an existing list. List comprehensions are enclosed within square brackets. When you\u2019re working with lists, you may want to create a&hellip;","protected":false},"author":240,"featured_media":1163,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-17706","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python List Comprehension: A How-To Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"List comprehensions allow you to create lists from an existing list. On Career Karma, learn how to use Python list comprehensions in your code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python List Comprehension: A How-To Guide\" \/>\n<meta property=\"og:description\" content=\"List comprehensions allow you to create lists from an existing list. On Career Karma, learn how to use Python list comprehensions in your code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-19T13:44:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:08:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python List Comprehension: A How-To Guide\",\"datePublished\":\"2021-01-19T13:44:44+00:00\",\"dateModified\":\"2023-12-01T12:08:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\"},\"wordCount\":1210,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\",\"name\":\"Python List Comprehension: A How-To Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg\",\"datePublished\":\"2021-01-19T13:44:44+00:00\",\"dateModified\":\"2023-12-01T12:08:12+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"List comprehensions allow you to create lists from an existing list. On Career Karma, learn how to use Python list comprehensions in your code.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg\",\"width\":1200,\"height\":800,\"caption\":\"Two guys coding\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python List Comprehension: A How-To Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python List Comprehension: A How-To Guide | Career Karma","description":"List comprehensions allow you to create lists from an existing list. On Career Karma, learn how to use Python list comprehensions in your code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/","og_locale":"en_US","og_type":"article","og_title":"Python List Comprehension: A How-To Guide","og_description":"List comprehensions allow you to create lists from an existing list. On Career Karma, learn how to use Python list comprehensions in your code.","og_url":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2021-01-19T13:44:44+00:00","article_modified_time":"2023-12-01T12:08:12+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python List Comprehension: A How-To Guide","datePublished":"2021-01-19T13:44:44+00:00","dateModified":"2023-12-01T12:08:12+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/"},"wordCount":1210,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/","url":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/","name":"Python List Comprehension: A How-To Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg","datePublished":"2021-01-19T13:44:44+00:00","dateModified":"2023-12-01T12:08:12+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"List comprehensions allow you to create lists from an existing list. On Career Karma, learn how to use Python list comprehensions in your code.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-list-comprehension\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/03\/alvaro-reyes-500044-unsplash.jpg","width":1200,"height":800,"caption":"Two guys coding"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-list-comprehension\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python List Comprehension: A How-To Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/17706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=17706"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/17706\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/1163"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=17706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=17706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=17706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}