

{"id":6057,"date":"2018-01-18T08:45:32","date_gmt":"2018-01-18T03:15:32","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6057"},"modified":"2026-04-28T14:39:31","modified_gmt":"2026-04-28T09:09:31","slug":"python-list-comprehension","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/","title":{"rendered":"Python List Comprehension with Syntax &amp; Examples"},"content":{"rendered":"<p>Previously, we discussed <strong><a href=\"https:\/\/data-flair.training\/blogs\/python-lists-examples\/\">Lists in Python<\/a>.<\/strong> In this tutorial, we&#8217;ll discuss what Python list comprehension is and how to use it.<\/p>\n<p>Along with this, we will learn syntax, list comprehension vs lambda expression in Python 3. Along with this, we will study conditionals and nested list comprehension in the Python Programming Language.<\/p>\n<p>So, let&#8217;s begin the Python list comprehension Tutorial.<\/p>\n<h3>What is Python List Comprehension?<\/h3>\n<p>To get Python list comprehensions back into memory, we\u2019ll take a quick example.<\/p>\n<p>Suppose you want to take the letters in the word \u2018anxiety\u2019 and want to put them in a list. Remember that a Python string is iterable.<\/p>\n<p><strong>Uses of list comprehension in Python:<\/strong><\/p>\n<ul>\n<li>It combines changes, looping, and filtering all in one line.<\/li>\n<li>It avoids repetitiveness of words in a loop to make the code simpler to read.<\/li>\n<li>It runs faster than other codes.<\/li>\n<\/ul>\n<p>Using a for loop, you would:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; mylist=[]\r\n&gt;&gt;&gt; for i in 'anxiety':\r\n          mylist.append(i)\r\n&gt;&gt;&gt; mylist<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;a&#8217;, &#8216;n&#8217;, &#8216;x&#8217;, &#8216;i&#8217;, &#8216;e&#8217;, &#8216;t&#8217;, &#8216;y&#8217;]<\/div>\n<p>But with a Python list comprehension, you can do this in one line:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [i for i in 'anxiety']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;a&#8217;, &#8216;n&#8217;, &#8216;x&#8217;, &#8216;i&#8217;, &#8216;e&#8217;, &#8216;t&#8217;, &#8216;y&#8217;]<\/div>\n<p>Now that we\u2019ve got your interest, we\u2019ll dig a little deeper.<\/p>\n<h3>Syntax of List Comprehension in Python<\/h3>\n<p>For a Python list comprehension, we use the delimiters for a list- square brackets.<\/p>\n<p>Inside those, we use a for-statement on an iterable (a list, here). We\u2019ll take an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [i*2 for i in {3,1,2}]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[2, 4, 6]<\/div>\n<p>Here, we return twice every value in the set {3,1,2} into a new list.<\/p>\n<p>So we guess we can state the syntax for a Python list comprehension as follows:<\/p>\n<p>[expression for item in list]<\/p>\n<p>Note that not every loop has an equivalent list comprehension in Python.<\/p>\n<h3>Python List Comprehension vs Lambda Expression<\/h3>\n<p>Something about this syntax leaves us nostalgic. Remember when we learned about Python lambda expressions in an earlier lesson?<\/p>\n<p>A Python 3 lambda expression returns a certain expression\u2019s value, which it calculates using values of the arguments it receives.<\/p>\n<p>Let\u2019s create a list from a set using the list comprehension syntax.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; myset={3,1,2}\r\n&gt;&gt;&gt; makelist=lambda i:list(i)\r\n&gt;&gt;&gt; mylist=makelist(myset)\r\n&gt;&gt;&gt; mylist<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3]<\/div>\n<p>Here, we first took a set {3,1,2}. Like you\u2019re aware by now, it rearranges itself as {1,2,3}. Then, we defined a lambda function, and stored it in the variable \u2018makelist\u2019.<\/p>\n<p>This lambda function takes a value, converts it into a list, and returns it. Finally, we called makelist on the set myset, and stored it in the variable mylist, which now holds a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; type(mylist)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;list&#8217;&gt;<\/div>\n<p>To do this using the map function instead, we write the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(map(lambda i:i,myset))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3]<\/div>\n<p>This code first takes a lambda expression: For each value i, it returns i and maps this to each value in the set myset. Next, it converts this into a Python list and prints it.<\/p>\n<p>A list comprehension\u2019s advantage over a lambda function is that it is more readable. Try reading both, and see for yourself.<\/p>\n<h3>Conditionals in Python List Comprehension<\/h3>\n<p>So far, you know that we use a for statement to declare our intentions.<\/p>\n<p>But did you know that it is possible to add a condition to this?<\/p>\n<p>This will add only those items to the list that meet the condition (for which the condition is True).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [i for i in range(8) if i%2!=0]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 3, 5, 7]<\/div>\n<p>This code takes the values in the range(8), i.e., 0 to 7, and adds the odd values to a list.<\/p>\n<h4>a. Nested Conditionals<\/h4>\n<p>With a Python list comprehension, it doesn\u2019t have to be a single condition; you can nest conditions. Here\u2019s an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [i for i in range(8) if i%2==0 if i%3==0]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[0, 6]<\/div>\n<p>Let\u2019s see how this works. For integers 0 to 7, it first filters out the elements that aren\u2019t perfectly divisible by 2.<\/p>\n<p>For the remaining elements, it keeps only those that are divisible by 3. Finally, it stores these elements in a list, and prints it out.<\/p>\n<p>Remember, this is a nested conditional, not an AND operation of two conditions.<\/p>\n<h4>b. if..else in List Comprehension in Python<\/h4>\n<p>You can also use an if-else in a list comprehension in Python. Since in a comprehension, the first thing we specify is the value to put in a list, this is where we put our if-else.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [\"Even\" if i%2==0 else \"Odd\" for i in range(8)]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;Even&#8217;, &#8216;Odd&#8217;, &#8216;Even&#8217;, &#8216;Odd&#8217;, &#8216;Even&#8217;, &#8216;Odd&#8217;, &#8216;Even&#8217;, &#8216;Odd&#8217;]<\/div>\n<p>This code stores in a list, for each integer from 0 to 7, whether it is even or odd.<\/p>\n<p>Try using different conditions with this one, and tell us in the comments.<\/p>\n<h3>Nested List Comprehension in Python<\/h3>\n<p>Finally, in this tutorial, we will end by discussing how to use a Python list comprehension for a nested for-loop.<\/p>\n<p>Let\u2019s take some code to print the tables of numbers 7 and 8. Using regular for-loops, we\u2019d write the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in range(7,9):\r\n        for j in range(1,11):\r\n               print(f\"{i}*{j}={i*j}\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>7*1=77*2=147*3=21<\/p>\n<p>7*4=28<\/p>\n<p>7*5=35<\/p>\n<p>7*6=42<\/p>\n<p>7*7=49<\/p>\n<p>7*8=56<\/p>\n<p>7*9=63<\/p>\n<p>7*10=70<\/p>\n<p>8*1=8<\/p>\n<p>8*2=16<\/p>\n<p>8*3=24<\/p>\n<p>8*4=32<\/p>\n<p>8*5=40<\/p>\n<p>8*6=48<\/p>\n<p>8*7=56<\/p>\n<p>8*8=64<\/p>\n<p>8*9=72<\/p>\n<p>8*10=80<\/p>\n<\/div>\n<p>To do this using a Python list comprehension, however, we use the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [[i*j for j in range(1,11)] for i in range(7,9)]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[7, 14, 21, 28, 35, 42, 49, 56, 63, 70], [8, 16, 24, 32, 40, 48, 56, 64, 72, 80]]<\/div>\n<p>We used the for-loop for j as the inner comprehension, because it is the inner loop in the previous code.<\/p>\n<p>So, this was all about Python List Comprehension Tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on List Comprehension<\/h3>\n<p>1. What is a Python list comprehension?<\/p>\n<p>2. How is Python list comprehension useful?<\/p>\n<p>3. How does list comprehension work in Python?<\/p>\n<p>4. Give an example of Python list comprehension<\/p>\n<p>5. Are list comprehensions faster in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>Now that you do better with Python list comprehensions, we hope you\u2019ll make good use of them for speed and readability.<\/p>\n<p>However, it makes no sense to write a very long and complicated list comprehension.<\/p>\n<p>Also, you can write a for-loop for every list comprehension in Python, but you can\u2019t write list comprehensions for very complex for-loops.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously, we discussed Lists in Python. In this tutorial, we&#8217;ll discuss what Python list comprehension is and how to use it. Along with this, we will learn syntax, list comprehension vs lambda expression in&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":36008,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[8318,8319,10643],"class_list":["post-6057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-list-comprehension-in-python","tag-list-comprehension-vs-lambda-expression-in-python","tag-python-list-comprehension"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python List Comprehension with Syntax &amp; Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is list comprehension in python with syntax and examples, list comprehension vs lambda expressions, loop in list comprehension\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/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 with Syntax &amp; Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is list comprehension in python with syntax and examples, list comprehension vs lambda expressions, loop in list comprehension\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-18T03:15:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:09:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-List-Comprehension-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python List Comprehension with Syntax &amp; Examples - DataFlair","description":"Learn what is list comprehension in python with syntax and examples, list comprehension vs lambda expressions, loop in list comprehension","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:\/\/data-flair.training\/blogs\/python-list-comprehension\/","og_locale":"en_US","og_type":"article","og_title":"Python List Comprehension with Syntax &amp; Examples - DataFlair","og_description":"Learn what is list comprehension in python with syntax and examples, list comprehension vs lambda expressions, loop in list comprehension","og_url":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-18T03:15:32+00:00","article_modified_time":"2026-04-28T09:09:31+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-List-Comprehension-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python List Comprehension with Syntax &amp; Examples","datePublished":"2018-01-18T03:15:32+00:00","dateModified":"2026-04-28T09:09:31+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/"},"wordCount":912,"commentCount":6,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-List-Comprehension-1.jpg","keywords":["list comprehension in python","list comprehension vs lambda expression in python","python list comprehension"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/","url":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/","name":"Python List Comprehension with Syntax &amp; Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-List-Comprehension-1.jpg","datePublished":"2018-01-18T03:15:32+00:00","dateModified":"2026-04-28T09:09:31+00:00","description":"Learn what is list comprehension in python with syntax and examples, list comprehension vs lambda expressions, loop in list comprehension","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-list-comprehension\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-List-Comprehension-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-List-Comprehension-1.jpg","width":1200,"height":628,"caption":"Python List Comprehension with Syntax and Examples"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-list-comprehension\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python List Comprehension with Syntax &amp; Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=6057"}],"version-history":[{"count":19,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6057\/revisions"}],"predecessor-version":[{"id":148007,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6057\/revisions\/148007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/36008"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}