{"id":77,"date":"2026-03-01T17:02:12","date_gmt":"2026-03-01T12:02:12","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=77"},"modified":"2026-03-13T13:00:31","modified_gmt":"2026-03-13T08:00:31","slug":"list-comprehension","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/","title":{"rendered":"List Comprehension"},"content":{"rendered":"\n<p><strong>List comprehension<\/strong> is a short and powerful way to create lists in Python.<br>It allows you to generate a new list by writing a single line of code instead of using multiple lines with a loop.<\/p>\n\n\n\n<p>It makes your code cleaner, shorter, and more readable.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>BASIC SYNTAX<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">[expression for item in iterable]<\/pre>\n\n\n\n<p>\u2022 <code>expression<\/code> \u2192 What you want to store in the list<br>\u2022 <code>item<\/code> \u2192 Variable representing each element<br>\u2022 <code>iterable<\/code> \u2192 Sequence like list, range, string, etc.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>EXAMPLE 1: SIMPLE LIST CREATION<\/strong><\/h1>\n\n\n\n<p>Using normal loop:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">squares = []<br>for x in range(5):<br>    squares.append(x * x)print(squares)<\/pre>\n\n\n\n<p>Using list comprehension:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">squares = [x * x for x in range(5)]<br>print(squares)<\/pre>\n\n\n\n<p>Both give the same result.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>LIST COMPREHENSION WITH CONDITION<\/strong><\/h1>\n\n\n\n<p>You can add an <code>if<\/code> condition.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">even_numbers = [x for x in range(10) if x % 2 == 0]<br>print(even_numbers)<\/pre>\n\n\n\n<p>This creates a list of even numbers.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>USING IF-ELSE IN LIST COMPREHENSION<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">result = [\"Even\" if x % 2 == 0 else \"Odd\" for x in range(5)]<br>print(result)<\/pre>\n\n\n\n<p>This checks each number and labels it as Even or Odd.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>LIST COMPREHENSION WITH STRINGS<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">word = \"Python\"<br>letters = [letter for letter in word]<br>print(letters)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>NESTED LIST COMPREHENSION<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">matrix = [[j for j in range(3)] for i in range(3)]<br>print(matrix)<\/pre>\n\n\n\n<p>This creates a 3\u00d73 matrix.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY USE LIST COMPREHENSION<\/strong><\/h1>\n\n\n\n<p>\u2022 Makes code shorter<br>\u2022 Improves readability<br>\u2022 Faster than traditional loops in many cases<br>\u2022 Easy to apply conditions<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Keep it simple for readability<br>\u2022 Avoid very complex nested comprehensions<br>\u2022 Useful for transforming and filtering data<\/p>\n\n\n\n<p>Understanding <strong>list comprehension<\/strong> helps you write efficient and professional Python code.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > Data Structures > List Comprehension<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773388846715\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":30,"template":"","class_list":["post-77","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>List Comprehension - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python list comprehension: create, filter, and transform lists in one line, with conditions, nested lists, and cleaner 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:\/\/gigz.pk\/python\/lesson\/list-comprehension\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"List Comprehension - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python list comprehension: create, filter, and transform lists in one line, with conditions, nested lists, and cleaner code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T08:00:31+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/list-comprehension\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/list-comprehension\\\/\",\"name\":\"List Comprehension - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:02:12+00:00\",\"dateModified\":\"2026-03-13T08:00:31+00:00\",\"description\":\"Learn Python list comprehension: create, filter, and transform lists in one line, with conditions, nested lists, and cleaner code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/list-comprehension\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/list-comprehension\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/list-comprehension\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Data Structures > List Comprehension\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"List Comprehension - One Language. Endless Possibilities","description":"Learn Python list comprehension: create, filter, and transform lists in one line, with conditions, nested lists, and cleaner 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:\/\/gigz.pk\/python\/lesson\/list-comprehension\/","og_locale":"en_US","og_type":"article","og_title":"List Comprehension - One Language. Endless Possibilities","og_description":"Learn Python list comprehension: create, filter, and transform lists in one line, with conditions, nested lists, and cleaner code.","og_url":"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T08:00:31+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/","url":"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/","name":"List Comprehension - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:02:12+00:00","dateModified":"2026-03-13T08:00:31+00:00","description":"Learn Python list comprehension: create, filter, and transform lists in one line, with conditions, nested lists, and cleaner code.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/list-comprehension\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Data Structures > List Comprehension"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}