CINXE.COM
List comprehension - Wikipedia
<!DOCTYPE html> <html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-enabled skin-theme-clientpref-day vector-sticky-header-enabled vector-toc-available" lang="en" dir="ltr"> <head> <meta charset="UTF-8"> <title>List comprehension - Wikipedia</title> <script>(function(){var className="client-js vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-enabled skin-theme-clientpref-day vector-sticky-header-enabled vector-toc-available";var cookie=document.cookie.match(/(?:^|; )enwikimwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy", "wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"7745a48a-9915-4b42-b838-7cfd7bd8ea1f","wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"List_comprehension","wgTitle":"List comprehension","wgCurRevisionId":1228787071,"wgRevisionId":1228787071,"wgArticleId":275744,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Articles with short description","Short description is different from Wikidata","Articles with example code","Articles with example Haskell code","Articles with example Python (programming language) code","Articles with example Racket code","Programming constructs"],"wgPageViewLanguage":"en","wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"List_comprehension","wgRelevantArticleId":275744,"wgIsProbablyEditable":true, "wgRelevantPageIsProbablyEditable":true,"wgRestrictionEdit":[],"wgRestrictionMove":[],"wgNoticeProject":"wikipedia","wgCiteReferencePreviewsActive":false,"wgFlaggedRevsParams":{"tags":{"status":{"levels":1}}},"wgMediaViewerOnClick":true,"wgMediaViewerEnabledByDefault":true,"wgPopupsFlags":0,"wgVisualEditor":{"pageLanguageCode":"en","pageLanguageDir":"ltr","pageVariantFallbacks":"en"},"wgMFDisplayWikibaseDescriptions":{"search":true,"watchlist":true,"tagline":false,"nearby":true},"wgWMESchemaEditAttemptStepOversample":false,"wgWMEPageLength":20000,"wgEditSubmitButtonLabelPublish":true,"wgULSPosition":"interlanguage","wgULSisCompactLinksEnabled":false,"wgVector2022LanguageInHeader":true,"wgULSisLanguageSelectorEmpty":false,"wgWikibaseItemId":"Q795065","wgCheckUserClientHintsHeadersJsApi":["brands","architecture","bitness","fullVersionList","mobile","model","platform","platformVersion"],"GEHomepageSuggestedEditsEnableTopics":true,"wgGETopicsMatchModeEnabled":false, "wgGEStructuredTaskRejectionReasonTextInputEnabled":false,"wgGELevelingUpEnabledForUser":false};RLSTATE={"ext.globalCssJs.user.styles":"ready","site.styles":"ready","user.styles":"ready","ext.globalCssJs.user":"ready","user":"ready","user.options":"loading","ext.math.styles":"ready","ext.pygments":"ready","ext.cite.styles":"ready","skins.vector.search.codex.styles":"ready","skins.vector.styles":"ready","skins.vector.icons":"ready","ext.wikimediamessages.styles":"ready","ext.visualEditor.desktopArticleTarget.noscript":"ready","ext.uls.interlanguage":"ready","wikibase.client.init":"ready","ext.wikimediaBadges":"ready"};RLPAGEMODULES=["ext.pygments.view","ext.cite.ux-enhancements","site","mediawiki.page.ready","mediawiki.toc","skins.vector.js","ext.centralNotice.geoIP","ext.centralNotice.startUp","ext.gadget.ReferenceTooltips","ext.gadget.switcher","ext.urlShortener.toolbar","ext.centralauth.centralautologin","ext.popups","ext.visualEditor.desktopArticleTarget.init", "ext.visualEditor.targetLoader","ext.echo.centralauth","ext.eventLogging","ext.wikimediaEvents","ext.navigationTiming","ext.uls.interface","ext.cx.eventlogging.campaigns","ext.cx.uls.quick.actions","wikibase.client.vector-2022","ext.checkUser.clientHints","ext.growthExperiments.SuggestedEditSession"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/w/load.php?lang=en&modules=ext.cite.styles%7Cext.math.styles%7Cext.pygments%2CwikimediaBadges%7Cext.uls.interlanguage%7Cext.visualEditor.desktopArticleTarget.noscript%7Cext.wikimediamessages.styles%7Cskins.vector.icons%2Cstyles%7Cskins.vector.search.codex.styles%7Cwikibase.client.init&only=styles&skin=vector-2022"> <script async="" src="/w/load.php?lang=en&modules=startup&only=scripts&raw=1&skin=vector-2022"></script> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/w/load.php?lang=en&modules=site.styles&only=styles&skin=vector-2022"> <meta name="generator" content="MediaWiki 1.44.0-wmf.16"> <meta name="referrer" content="origin"> <meta name="referrer" content="origin-when-cross-origin"> <meta name="robots" content="max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=1120"> <meta property="og:title" content="List comprehension - Wikipedia"> <meta property="og:type" content="website"> <link rel="alternate" media="only screen and (max-width: 640px)" href="//en.m.wikipedia.org/wiki/List_comprehension"> <link rel="alternate" type="application/x-wiki" title="Edit this page" href="/w/index.php?title=List_comprehension&action=edit"> <link rel="apple-touch-icon" href="/static/apple-touch/wikipedia.png"> <link rel="icon" href="/static/favicon/wikipedia.ico"> <link rel="search" type="application/opensearchdescription+xml" href="/w/rest.php/v1/search" title="Wikipedia (en)"> <link rel="EditURI" type="application/rsd+xml" href="//en.wikipedia.org/w/api.php?action=rsd"> <link rel="canonical" href="https://en.wikipedia.org/wiki/List_comprehension"> <link rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en"> <link rel="alternate" type="application/atom+xml" title="Wikipedia Atom feed" href="/w/index.php?title=Special:RecentChanges&feed=atom"> <link rel="dns-prefetch" href="//meta.wikimedia.org" /> <link rel="dns-prefetch" href="login.wikimedia.org"> </head> <body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject mw-editable page-List_comprehension rootpage-List_comprehension skin-vector-2022 action-view"><a class="mw-jump-link" href="#bodyContent">Jump to content</a> <div class="vector-header-container"> <header class="vector-header mw-header"> <div class="vector-header-start"> <nav class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-dropdown" class="vector-dropdown vector-main-menu-dropdown vector-button-flush-left vector-button-flush-right" title="Main menu" > <input type="checkbox" id="vector-main-menu-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-main-menu-dropdown" class="vector-dropdown-checkbox " aria-label="Main menu" > <label id="vector-main-menu-dropdown-label" for="vector-main-menu-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-menu mw-ui-icon-wikimedia-menu"></span> <span class="vector-dropdown-label-text">Main menu</span> </label> <div class="vector-dropdown-content"> <div id="vector-main-menu-unpinned-container" class="vector-unpinned-container"> <div id="vector-main-menu" class="vector-main-menu vector-pinnable-element"> <div class="vector-pinnable-header vector-main-menu-pinnable-header vector-pinnable-header-unpinned" data-feature-name="main-menu-pinned" data-pinnable-element-id="vector-main-menu" data-pinned-container-id="vector-main-menu-pinned-container" data-unpinned-container-id="vector-main-menu-unpinned-container" > <div class="vector-pinnable-header-label">Main menu</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-main-menu.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-main-menu.unpin">hide</button> </div> <div id="p-navigation" class="vector-menu mw-portlet mw-portlet-navigation" > <div class="vector-menu-heading"> Navigation </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-mainpage-description" class="mw-list-item"><a href="/wiki/Main_Page" title="Visit the main page [z]" accesskey="z"><span>Main page</span></a></li><li id="n-contents" class="mw-list-item"><a href="/wiki/Wikipedia:Contents" title="Guides to browsing Wikipedia"><span>Contents</span></a></li><li id="n-currentevents" class="mw-list-item"><a href="/wiki/Portal:Current_events" title="Articles related to current events"><span>Current events</span></a></li><li id="n-randompage" class="mw-list-item"><a href="/wiki/Special:Random" title="Visit a randomly selected article [x]" accesskey="x"><span>Random article</span></a></li><li id="n-aboutsite" class="mw-list-item"><a href="/wiki/Wikipedia:About" title="Learn about Wikipedia and how it works"><span>About Wikipedia</span></a></li><li id="n-contactpage" class="mw-list-item"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us" title="How to contact Wikipedia"><span>Contact us</span></a></li> </ul> </div> </div> <div id="p-interaction" class="vector-menu mw-portlet mw-portlet-interaction" > <div class="vector-menu-heading"> Contribute </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-help" class="mw-list-item"><a href="/wiki/Help:Contents" title="Guidance on how to use and edit Wikipedia"><span>Help</span></a></li><li id="n-introduction" class="mw-list-item"><a href="/wiki/Help:Introduction" title="Learn how to edit Wikipedia"><span>Learn to edit</span></a></li><li id="n-portal" class="mw-list-item"><a href="/wiki/Wikipedia:Community_portal" title="The hub for editors"><span>Community portal</span></a></li><li id="n-recentchanges" class="mw-list-item"><a href="/wiki/Special:RecentChanges" title="A list of recent changes to Wikipedia [r]" accesskey="r"><span>Recent changes</span></a></li><li id="n-upload" class="mw-list-item"><a href="/wiki/Wikipedia:File_upload_wizard" title="Add images or other media for use on Wikipedia"><span>Upload file</span></a></li><li id="n-specialpages" class="mw-list-item"><a href="/wiki/Special:SpecialPages"><span>Special pages</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> <a href="/wiki/Main_Page" class="mw-logo"> <img class="mw-logo-icon" src="/static/images/icons/wikipedia.png" alt="" aria-hidden="true" height="50" width="50"> <span class="mw-logo-container skin-invert"> <img class="mw-logo-wordmark" alt="Wikipedia" src="/static/images/mobile/copyright/wikipedia-wordmark-en.svg" style="width: 7.5em; height: 1.125em;"> <img class="mw-logo-tagline" alt="The Free Encyclopedia" src="/static/images/mobile/copyright/wikipedia-tagline-en.svg" width="117" height="13" style="width: 7.3125em; height: 0.8125em;"> </span> </a> </div> <div class="vector-header-end"> <div id="p-search" role="search" class="vector-search-box-vue vector-search-box-collapses vector-search-box-show-thumbnail vector-search-box-auto-expand-width vector-search-box"> <a href="/wiki/Special:Search" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only search-toggle" title="Search Wikipedia [f]" accesskey="f"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>Search</span> </a> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail cdx-typeahead-search--auto-expand-width"> <form action="/w/index.php" id="searchform" class="cdx-search-input cdx-search-input--has-end-button"> <div id="simpleSearch" class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="Search Wikipedia" aria-label="Search Wikipedia" autocapitalize="sentences" title="Search Wikipedia [f]" accesskey="f" id="searchInput" > <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="Special:Search"> </div> <button class="cdx-button cdx-search-input__end-button">Search</button> </form> </div> </div> </div> <nav class="vector-user-links vector-user-links-wide" aria-label="Personal tools"> <div class="vector-user-links-main"> <div id="p-vector-user-menu-preferences" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-userpage" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-dropdown" class="vector-dropdown " title="Change the appearance of the page's font size, width, and color" > <input type="checkbox" id="vector-appearance-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-appearance-dropdown" class="vector-dropdown-checkbox " aria-label="Appearance" > <label id="vector-appearance-dropdown-label" for="vector-appearance-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-appearance mw-ui-icon-wikimedia-appearance"></span> <span class="vector-dropdown-label-text">Appearance</span> </label> <div class="vector-dropdown-content"> <div id="vector-appearance-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <div id="p-vector-user-menu-notifications" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-overflow" class="vector-menu mw-portlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikipedia.org&uselang=en" class=""><span>Donate</span></a> </li> <li id="pt-createaccount-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=Special:CreateAccount&returnto=List+comprehension" title="You are encouraged to create an account and log in; however, it is not mandatory" class=""><span>Create account</span></a> </li> <li id="pt-login-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=Special:UserLogin&returnto=List+comprehension" title="You're encouraged to log in; however, it's not mandatory. [o]" accesskey="o" class=""><span>Log in</span></a> </li> </ul> </div> </div> </div> <div id="vector-user-links-dropdown" class="vector-dropdown vector-user-menu vector-button-flush-right vector-user-menu-logged-out" title="Log in and more options" > <input type="checkbox" id="vector-user-links-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-user-links-dropdown" class="vector-dropdown-checkbox " aria-label="Personal tools" > <label id="vector-user-links-dropdown-label" for="vector-user-links-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-ellipsis mw-ui-icon-wikimedia-ellipsis"></span> <span class="vector-dropdown-label-text">Personal tools</span> </label> <div class="vector-dropdown-content"> <div id="p-personal" class="vector-menu mw-portlet mw-portlet-personal user-links-collapsible-item" title="User menu" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport" class="user-links-collapsible-item mw-list-item"><a href="https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikipedia.org&uselang=en"><span>Donate</span></a></li><li id="pt-createaccount" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=Special:CreateAccount&returnto=List+comprehension" title="You are encouraged to create an account and log in; however, it is not mandatory"><span class="vector-icon mw-ui-icon-userAdd mw-ui-icon-wikimedia-userAdd"></span> <span>Create account</span></a></li><li id="pt-login" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=Special:UserLogin&returnto=List+comprehension" title="You're encouraged to log in; however, it's not mandatory. [o]" accesskey="o"><span class="vector-icon mw-ui-icon-logIn mw-ui-icon-wikimedia-logIn"></span> <span>Log in</span></a></li> </ul> </div> </div> <div id="p-user-menu-anon-editor" class="vector-menu mw-portlet mw-portlet-user-menu-anon-editor" > <div class="vector-menu-heading"> Pages for logged out editors <a href="/wiki/Help:Introduction" aria-label="Learn more about editing"><span>learn more</span></a> </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-anoncontribs" class="mw-list-item"><a href="/wiki/Special:MyContributions" title="A list of edits made from this IP address [y]" accesskey="y"><span>Contributions</span></a></li><li id="pt-anontalk" class="mw-list-item"><a href="/wiki/Special:MyTalk" title="Discussion about edits from this IP address [n]" accesskey="n"><span>Talk</span></a></li> </ul> </div> </div> </div> </div> </nav> </div> </header> </div> <div class="mw-page-container"> <div class="mw-page-container-inner"> <div class="vector-sitenotice-container"> <div id="siteNotice"><!-- CentralNotice --></div> </div> <div class="vector-column-start"> <div class="vector-main-menu-container"> <div id="mw-navigation"> <nav id="mw-panel" class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-pinned-container" class="vector-pinned-container"> </div> </nav> </div> </div> <div class="vector-sticky-pinned-container"> <nav id="mw-panel-toc" aria-label="Contents" data-event-name="ui.sidebar-toc" class="mw-table-of-contents-container vector-toc-landmark"> <div id="vector-toc-pinned-container" class="vector-pinned-container"> <div id="vector-toc" class="vector-toc vector-pinnable-element"> <div class="vector-pinnable-header vector-toc-pinnable-header vector-pinnable-header-pinned" data-feature-name="toc-pinned" data-pinnable-element-id="vector-toc" > <h2 class="vector-pinnable-header-label">Contents</h2> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-toc.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-toc.unpin">hide</button> </div> <ul class="vector-toc-contents" id="mw-panel-toc-list"> <li id="toc-mw-content-text" class="vector-toc-list-item vector-toc-level-1"> <a href="#" class="vector-toc-link"> <div class="vector-toc-text">(Top)</div> </a> </li> <li id="toc-Overview" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Overview"> <div class="vector-toc-text"> <span class="vector-toc-numb">1</span> <span>Overview</span> </div> </a> <ul id="toc-Overview-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-History" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#History"> <div class="vector-toc-text"> <span class="vector-toc-numb">2</span> <span>History</span> </div> </a> <ul id="toc-History-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Examples_in_different_programming_languages" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Examples_in_different_programming_languages"> <div class="vector-toc-text"> <span class="vector-toc-numb">3</span> <span>Examples in different programming languages</span> </div> </a> <ul id="toc-Examples_in_different_programming_languages-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Similar_constructs" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Similar_constructs"> <div class="vector-toc-text"> <span class="vector-toc-numb">4</span> <span>Similar constructs</span> </div> </a> <button aria-controls="toc-Similar_constructs-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Similar constructs subsection</span> </button> <ul id="toc-Similar_constructs-sublist" class="vector-toc-list"> <li id="toc-Monad_comprehension" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Monad_comprehension"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.1</span> <span>Monad comprehension</span> </div> </a> <ul id="toc-Monad_comprehension-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Set_comprehension" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Set_comprehension"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.2</span> <span>Set comprehension</span> </div> </a> <ul id="toc-Set_comprehension-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Dictionary_comprehension" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Dictionary_comprehension"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.3</span> <span>Dictionary comprehension</span> </div> </a> <ul id="toc-Dictionary_comprehension-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Parallel_list_comprehension" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Parallel_list_comprehension"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.4</span> <span>Parallel list comprehension</span> </div> </a> <ul id="toc-Parallel_list_comprehension-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-XQuery_and_XPath" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#XQuery_and_XPath"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.5</span> <span>XQuery and XPath</span> </div> </a> <ul id="toc-XQuery_and_XPath-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-LINQ_in_C#" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#LINQ_in_C#"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.6</span> <span>LINQ in C#</span> </div> </a> <ul id="toc-LINQ_in_C#-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-C++" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#C++"> <div class="vector-toc-text"> <span class="vector-toc-numb">4.7</span> <span>C++</span> </div> </a> <ul id="toc-C++-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-See_also" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#See_also"> <div class="vector-toc-text"> <span class="vector-toc-numb">5</span> <span>See also</span> </div> </a> <ul id="toc-See_also-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Notes_and_references" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#Notes_and_references"> <div class="vector-toc-text"> <span class="vector-toc-numb">6</span> <span>Notes and references</span> </div> </a> <ul id="toc-Notes_and_references-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-External_links" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#External_links"> <div class="vector-toc-text"> <span class="vector-toc-numb">7</span> <span>External links</span> </div> </a> <button aria-controls="toc-External_links-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle External links subsection</span> </button> <ul id="toc-External_links-sublist" class="vector-toc-list"> <li id="toc-Axiom" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Axiom"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.1</span> <span>Axiom</span> </div> </a> <ul id="toc-Axiom-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Clojure" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Clojure"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.2</span> <span>Clojure</span> </div> </a> <ul id="toc-Clojure-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Common_Lisp" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Common_Lisp"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.3</span> <span>Common Lisp</span> </div> </a> <ul id="toc-Common_Lisp-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Haskell" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Haskell"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.4</span> <span>Haskell</span> </div> </a> <ul id="toc-Haskell-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-OCaml" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#OCaml"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.5</span> <span>OCaml</span> </div> </a> <ul id="toc-OCaml-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Python" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Python"> <div class="vector-toc-text"> <span class="vector-toc-numb">7.6</span> <span>Python</span> </div> </a> <ul id="toc-Python-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> </ul> </div> </div> </nav> </div> </div> <div class="mw-content-container"> <main id="content" class="mw-body"> <header class="mw-body-header vector-page-titlebar"> <nav aria-label="Contents" class="vector-toc-landmark"> <div id="vector-page-titlebar-toc" class="vector-dropdown vector-page-titlebar-toc vector-button-flush-left" title="Table of Contents" > <input type="checkbox" id="vector-page-titlebar-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-titlebar-toc" class="vector-dropdown-checkbox " aria-label="Toggle the table of contents" > <label id="vector-page-titlebar-toc-label" for="vector-page-titlebar-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">Toggle the table of contents</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-titlebar-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">List comprehension</span></h1> <div id="p-lang-btn" class="vector-dropdown mw-portlet mw-portlet-lang" > <input type="checkbox" id="p-lang-btn-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-p-lang-btn" class="vector-dropdown-checkbox mw-interlanguage-selector" aria-label="Go to an article in another language. Available in 14 languages" > <label id="p-lang-btn-label" for="p-lang-btn-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--action-progressive mw-portlet-lang-heading-14" aria-hidden="true" ><span class="vector-icon mw-ui-icon-language-progressive mw-ui-icon-wikimedia-language-progressive"></span> <span class="vector-dropdown-label-text">14 languages</span> </label> <div class="vector-dropdown-content"> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li class="interlanguage-link interwiki-ar mw-list-item"><a href="https://ar.wikipedia.org/wiki/%D8%A7%D8%B4%D8%AA%D9%85%D8%A7%D9%84_%D9%82%D8%A7%D8%A6%D9%85%D8%A9_(%D8%A8%D8%B1%D9%85%D8%AC%D8%A9)" title="اشتمال قائمة (برمجة) – Arabic" lang="ar" hreflang="ar" data-title="اشتمال قائمة (برمجة)" data-language-autonym="العربية" data-language-local-name="Arabic" class="interlanguage-link-target"><span>العربية</span></a></li><li class="interlanguage-link interwiki-ca mw-list-item"><a href="https://ca.wikipedia.org/wiki/Llista_per_comprensi%C3%B3" title="Llista per comprensió – Catalan" lang="ca" hreflang="ca" data-title="Llista per comprensió" data-language-autonym="Català" data-language-local-name="Catalan" class="interlanguage-link-target"><span>Català</span></a></li><li class="interlanguage-link interwiki-de mw-list-item"><a href="https://de.wikipedia.org/wiki/List_Comprehension" title="List Comprehension – German" lang="de" hreflang="de" data-title="List Comprehension" data-language-autonym="Deutsch" data-language-local-name="German" class="interlanguage-link-target"><span>Deutsch</span></a></li><li class="interlanguage-link interwiki-et mw-list-item"><a href="https://et.wikipedia.org/wiki/Loendi_h%C3%B5lmamine" title="Loendi hõlmamine – Estonian" lang="et" hreflang="et" data-title="Loendi hõlmamine" data-language-autonym="Eesti" data-language-local-name="Estonian" class="interlanguage-link-target"><span>Eesti</span></a></li><li class="interlanguage-link interwiki-fr mw-list-item"><a href="https://fr.wikipedia.org/wiki/Liste_en_compr%C3%A9hension" title="Liste en compréhension – French" lang="fr" hreflang="fr" data-title="Liste en compréhension" data-language-autonym="Français" data-language-local-name="French" class="interlanguage-link-target"><span>Français</span></a></li><li class="interlanguage-link interwiki-ko mw-list-item"><a href="https://ko.wikipedia.org/wiki/%EB%A6%AC%EC%8A%A4%ED%8A%B8_%EC%BA%84%ED%94%84%EB%A6%AC%ED%97%A8%EC%85%98" title="리스트 캄프리헨션 – Korean" lang="ko" hreflang="ko" data-title="리스트 캄프리헨션" data-language-autonym="한국어" data-language-local-name="Korean" class="interlanguage-link-target"><span>한국어</span></a></li><li class="interlanguage-link interwiki-it mw-list-item"><a href="https://it.wikipedia.org/wiki/Comprensione_di_lista" title="Comprensione di lista – Italian" lang="it" hreflang="it" data-title="Comprensione di lista" data-language-autonym="Italiano" data-language-local-name="Italian" class="interlanguage-link-target"><span>Italiano</span></a></li><li class="interlanguage-link interwiki-nl mw-list-item"><a href="https://nl.wikipedia.org/wiki/Lijstcomprehensie" title="Lijstcomprehensie – Dutch" lang="nl" hreflang="nl" data-title="Lijstcomprehensie" data-language-autonym="Nederlands" data-language-local-name="Dutch" class="interlanguage-link-target"><span>Nederlands</span></a></li><li class="interlanguage-link interwiki-ja mw-list-item"><a href="https://ja.wikipedia.org/wiki/%E3%83%AA%E3%82%B9%E3%83%88%E5%86%85%E5%8C%85%E8%A1%A8%E8%A8%98" title="リスト内包表記 – Japanese" lang="ja" hreflang="ja" data-title="リスト内包表記" data-language-autonym="日本語" data-language-local-name="Japanese" class="interlanguage-link-target"><span>日本語</span></a></li><li class="interlanguage-link interwiki-pl mw-list-item"><a href="https://pl.wikipedia.org/wiki/Lista_sk%C5%82adana" title="Lista składana – Polish" lang="pl" hreflang="pl" data-title="Lista składana" data-language-autonym="Polski" data-language-local-name="Polish" class="interlanguage-link-target"><span>Polski</span></a></li><li class="interlanguage-link interwiki-pt mw-list-item"><a href="https://pt.wikipedia.org/wiki/Compreens%C3%A3o_de_lista" title="Compreensão de lista – Portuguese" lang="pt" hreflang="pt" data-title="Compreensão de lista" data-language-autonym="Português" data-language-local-name="Portuguese" class="interlanguage-link-target"><span>Português</span></a></li><li class="interlanguage-link interwiki-ru mw-list-item"><a href="https://ru.wikipedia.org/wiki/%D0%A1%D0%BF%D0%B8%D1%81%D0%BA%D0%BE%D0%B2%D0%BE%D0%B5_%D0%B2%D0%BA%D0%BB%D1%8E%D1%87%D0%B5%D0%BD%D0%B8%D0%B5" title="Списковое включение – Russian" lang="ru" hreflang="ru" data-title="Списковое включение" data-language-autonym="Русский" data-language-local-name="Russian" class="interlanguage-link-target"><span>Русский</span></a></li><li class="interlanguage-link interwiki-uk mw-list-item"><a href="https://uk.wikipedia.org/wiki/%D0%A1%D0%BF%D0%B8%D1%81%D0%BA%D0%BE%D0%B2%D1%96_%D0%B2%D0%B8%D1%80%D0%B0%D0%B7%D0%B8" title="Спискові вирази – Ukrainian" lang="uk" hreflang="uk" data-title="Спискові вирази" data-language-autonym="Українська" data-language-local-name="Ukrainian" class="interlanguage-link-target"><span>Українська</span></a></li><li class="interlanguage-link interwiki-zh mw-list-item"><a href="https://zh.wikipedia.org/wiki/%E5%88%97%E8%A1%A8%E6%8E%A8%E5%AF%BC%E5%BC%8F" title="列表推导式 – Chinese" lang="zh" hreflang="zh" data-title="列表推导式" data-language-autonym="中文" data-language-local-name="Chinese" class="interlanguage-link-target"><span>中文</span></a></li> </ul> <div class="after-portlet after-portlet-lang"><span class="wb-langlinks-edit wb-langlinks-link"><a href="https://www.wikidata.org/wiki/Special:EntityPage/Q795065#sitelinks-wikipedia" title="Edit interlanguage links" class="wbc-editpage">Edit links</a></span></div> </div> </div> </div> </header> <div class="vector-page-toolbar"> <div class="vector-page-toolbar-container"> <div id="left-navigation"> <nav aria-label="Namespaces"> <div id="p-associated-pages" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-associated-pages" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-nstab-main" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/List_comprehension" title="View the content page [c]" accesskey="c"><span>Article</span></a></li><li id="ca-talk" class="vector-tab-noicon mw-list-item"><a href="/wiki/Talk:List_comprehension" rel="discussion" title="Discuss improvements to the content page [t]" accesskey="t"><span>Talk</span></a></li> </ul> </div> </div> <div id="vector-variants-dropdown" class="vector-dropdown emptyPortlet" > <input type="checkbox" id="vector-variants-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-variants-dropdown" class="vector-dropdown-checkbox " aria-label="Change language variant" > <label id="vector-variants-dropdown-label" for="vector-variants-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">English</span> </label> <div class="vector-dropdown-content"> <div id="p-variants" class="vector-menu mw-portlet mw-portlet-variants emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </nav> </div> <div id="right-navigation" class="vector-collapsible"> <nav aria-label="Views"> <div id="p-views" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-views" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-view" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/List_comprehension"><span>Read</span></a></li><li id="ca-edit" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=List_comprehension&action=edit" title="Edit this page [e]" accesskey="e"><span>Edit</span></a></li><li id="ca-history" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=List_comprehension&action=history" title="Past revisions of this page [h]" accesskey="h"><span>View history</span></a></li> </ul> </div> </div> </nav> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-dropdown" class="vector-dropdown vector-page-tools-dropdown" > <input type="checkbox" id="vector-page-tools-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-tools-dropdown" class="vector-dropdown-checkbox " aria-label="Tools" > <label id="vector-page-tools-dropdown-label" for="vector-page-tools-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">Tools</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-tools-unpinned-container" class="vector-unpinned-container"> <div id="vector-page-tools" class="vector-page-tools vector-pinnable-element"> <div class="vector-pinnable-header vector-page-tools-pinnable-header vector-pinnable-header-unpinned" data-feature-name="page-tools-pinned" data-pinnable-element-id="vector-page-tools" data-pinned-container-id="vector-page-tools-pinned-container" data-unpinned-container-id="vector-page-tools-unpinned-container" > <div class="vector-pinnable-header-label">Tools</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-page-tools.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-page-tools.unpin">hide</button> </div> <div id="p-cactions" class="vector-menu mw-portlet mw-portlet-cactions emptyPortlet vector-has-collapsible-items" title="More options" > <div class="vector-menu-heading"> Actions </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-more-view" class="selected vector-more-collapsible-item mw-list-item"><a href="/wiki/List_comprehension"><span>Read</span></a></li><li id="ca-more-edit" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=List_comprehension&action=edit" title="Edit this page [e]" accesskey="e"><span>Edit</span></a></li><li id="ca-more-history" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=List_comprehension&action=history"><span>View history</span></a></li> </ul> </div> </div> <div id="p-tb" class="vector-menu mw-portlet mw-portlet-tb" > <div class="vector-menu-heading"> General </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-whatlinkshere" class="mw-list-item"><a href="/wiki/Special:WhatLinksHere/List_comprehension" title="List of all English Wikipedia pages containing links to this page [j]" accesskey="j"><span>What links here</span></a></li><li id="t-recentchangeslinked" class="mw-list-item"><a href="/wiki/Special:RecentChangesLinked/List_comprehension" rel="nofollow" title="Recent changes in pages linked from this page [k]" accesskey="k"><span>Related changes</span></a></li><li id="t-upload" class="mw-list-item"><a href="//en.wikipedia.org/wiki/Wikipedia:File_Upload_Wizard" title="Upload files [u]" accesskey="u"><span>Upload file</span></a></li><li id="t-permalink" class="mw-list-item"><a href="/w/index.php?title=List_comprehension&oldid=1228787071" title="Permanent link to this revision of this page"><span>Permanent link</span></a></li><li id="t-info" class="mw-list-item"><a href="/w/index.php?title=List_comprehension&action=info" title="More information about this page"><span>Page information</span></a></li><li id="t-cite" class="mw-list-item"><a href="/w/index.php?title=Special:CiteThisPage&page=List_comprehension&id=1228787071&wpFormIdentifier=titleform" title="Information on how to cite this page"><span>Cite this page</span></a></li><li id="t-urlshortener" class="mw-list-item"><a href="/w/index.php?title=Special:UrlShortener&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FList_comprehension"><span>Get shortened URL</span></a></li><li id="t-urlshortener-qrcode" class="mw-list-item"><a href="/w/index.php?title=Special:QrCode&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FList_comprehension"><span>Download QR code</span></a></li> </ul> </div> </div> <div id="p-coll-print_export" class="vector-menu mw-portlet mw-portlet-coll-print_export" > <div class="vector-menu-heading"> Print/export </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="coll-download-as-rl" class="mw-list-item"><a href="/w/index.php?title=Special:DownloadAsPdf&page=List_comprehension&action=show-download-screen" title="Download this page as a PDF file"><span>Download as PDF</span></a></li><li id="t-print" class="mw-list-item"><a href="/w/index.php?title=List_comprehension&printable=yes" title="Printable version of this page [p]" accesskey="p"><span>Printable version</span></a></li> </ul> </div> </div> <div id="p-wikibase-otherprojects" class="vector-menu mw-portlet mw-portlet-wikibase-otherprojects" > <div class="vector-menu-heading"> In other projects </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-wikibase" class="wb-otherproject-link wb-otherproject-wikibase-dataitem mw-list-item"><a href="https://www.wikidata.org/wiki/Special:EntityPage/Q795065" title="Structured data on this page hosted by Wikidata [g]" accesskey="g"><span>Wikidata item</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> </div> </div> </div> <div class="vector-column-end"> <div class="vector-sticky-pinned-container"> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-pinned-container" class="vector-pinned-container"> </div> </nav> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-pinned-container" class="vector-pinned-container"> <div id="vector-appearance" class="vector-appearance vector-pinnable-element"> <div class="vector-pinnable-header vector-appearance-pinnable-header vector-pinnable-header-pinned" data-feature-name="appearance-pinned" data-pinnable-element-id="vector-appearance" data-pinned-container-id="vector-appearance-pinned-container" data-unpinned-container-id="vector-appearance-unpinned-container" > <div class="vector-pinnable-header-label">Appearance</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-appearance.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-appearance.unpin">hide</button> </div> </div> </div> </nav> </div> </div> <div id="bodyContent" class="vector-body" aria-labelledby="firstHeading" data-mw-ve-target-container> <div class="vector-body-before-content"> <div class="mw-indicators"> </div> <div id="siteSub" class="noprint">From Wikipedia, the free encyclopedia</div> </div> <div id="contentSub"><div id="mw-content-subtitle"></div></div> <div id="mw-content-text" class="mw-body-content"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><div class="shortdescription nomobile noexcerpt noprint searchaux" style="display:none">Syntactic construct for creating a list based on existing lists</div> <p>A <b>list comprehension</b> is a <a href="/wiki/Syntax_of_programming_languages" class="mw-redirect" title="Syntax of programming languages">syntactic</a> construct available in some <a href="/wiki/Programming_language" title="Programming language">programming languages</a> for creating a list based on existing <a href="/wiki/List_(computing)" class="mw-redirect" title="List (computing)">lists</a>. It follows the form of the mathematical <i><a href="/wiki/Set-builder_notation" title="Set-builder notation">set-builder notation</a></i> (<i>set comprehension</i>) as distinct from the use of <a href="/wiki/Map_(higher-order_function)" title="Map (higher-order function)">map</a> and <a href="/wiki/Filter_(higher-order_function)" title="Filter (higher-order function)">filter</a> functions. </p> <meta property="mw:PageProp/toc" /> <div class="mw-heading mw-heading2"><h2 id="Overview">Overview</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=1" title="Edit section: Overview"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Consider the following example in mathematical <a href="/wiki/Set-builder_notation" title="Set-builder notation">set-builder notation</a>. </p> <dl><dd><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle S=\{2\cdot x\mid x\in \mathbb {N} ,\ x^{2}>3\}}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>S</mi> <mo>=</mo> <mo fence="false" stretchy="false">{</mo> <mn>2</mn> <mo>⋅<!-- ⋅ --></mo> <mi>x</mi> <mo>∣<!-- ∣ --></mo> <mi>x</mi> <mo>∈<!-- ∈ --></mo> <mrow class="MJX-TeXAtom-ORD"> <mi mathvariant="double-struck">N</mi> </mrow> <mo>,</mo> <mtext> </mtext> <msup> <mi>x</mi> <mrow class="MJX-TeXAtom-ORD"> <mn>2</mn> </mrow> </msup> <mo>></mo> <mn>3</mn> <mo fence="false" stretchy="false">}</mo> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle S=\{2\cdot x\mid x\in \mathbb {N} ,\ x^{2}>3\}}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/e6eafa5de185b3eeaab95c7ab27422b0b4d03e44" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:27.139ex; height:3.176ex;" alt="{\displaystyle S=\{2\cdot x\mid x\in \mathbb {N} ,\ x^{2}>3\}}"></span></dd></dl> <p>or often </p> <dl><dd><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle S=\{2\cdot x:x\in \mathbb {N} ,\ x^{2}>3\}}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>S</mi> <mo>=</mo> <mo fence="false" stretchy="false">{</mo> <mn>2</mn> <mo>⋅<!-- ⋅ --></mo> <mi>x</mi> <mo>:</mo> <mi>x</mi> <mo>∈<!-- ∈ --></mo> <mrow class="MJX-TeXAtom-ORD"> <mi mathvariant="double-struck">N</mi> </mrow> <mo>,</mo> <mtext> </mtext> <msup> <mi>x</mi> <mrow class="MJX-TeXAtom-ORD"> <mn>2</mn> </mrow> </msup> <mo>></mo> <mn>3</mn> <mo fence="false" stretchy="false">}</mo> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle S=\{2\cdot x:x\in \mathbb {N} ,\ x^{2}>3\}}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/d2e592426d233755041915864dc34aa7588b2b45" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:27.139ex; height:3.176ex;" alt="{\displaystyle S=\{2\cdot x:x\in \mathbb {N} ,\ x^{2}>3\}}"></span></dd></dl> <p>This can be read, "<span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle S}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>S</mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle S}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/4611d85173cd3b508e67077d4a1252c9c05abca2" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.499ex; height:2.176ex;" alt="{\displaystyle S}"></span> is the set of all numbers "2 times <span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>x</mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle x}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/87f9e315fd7e2ba406057a97300593c4802b53e4" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}"></span>" SUCH THAT <span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>x</mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle x}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/87f9e315fd7e2ba406057a97300593c4802b53e4" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}"></span> is an ELEMENT or MEMBER of the set of <a href="/wiki/Natural_number" title="Natural number">natural numbers</a> (<span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \mathbb {N} }"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mrow class="MJX-TeXAtom-ORD"> <mi mathvariant="double-struck">N</mi> </mrow> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle \mathbb {N} }</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/fdf9a96b565ea202d0f4322e9195613fb26a9bed" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.678ex; height:2.176ex;" alt="{\displaystyle \mathbb {N} }"></span>), AND <span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>x</mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle x}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/87f9e315fd7e2ba406057a97300593c4802b53e4" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}"></span> squared is greater than <span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 3}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mn>3</mn> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle 3}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/991e33c6e207b12546f15bdfee8b5726eafbbb2f" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.162ex; height:2.176ex;" alt="{\displaystyle 3}"></span>." </p><p>The smallest natural number, x = 1, fails to satisfy the condition x<sup>2</sup>>3 (the condition 1<sup>2</sup>>3 is false) so 2 ·1 is not included in S. The next natural number, 2, does satisfy the condition (2<sup>2</sup>>3) as does every other natural number. Thus x consists of 2, 3, 4, 5... Since the set <var style="padding-right: 1px;">S</var> consists of all numbers "2 times x" it is given by S = {4, 6, 8, 10,...}. S is, in other words, the set of all even numbers greater than 2. </p><p>In this annotated version of the example: </p> <dl><dd><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle S=\{\underbrace {2\cdot x} _{\color {Violet}{\text{output expression}}}\mid \underbrace {x} _{\color {Violet}{\text{variable}}}\in \underbrace {\mathbb {N} } _{\color {Violet}{\text{input set}}},\ \underbrace {x^{2}>3} _{\color {Violet}{\text{predicate}}}\}}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>S</mi> <mo>=</mo> <mo fence="false" stretchy="false">{</mo> <munder> <mrow class="MJX-TeXAtom-OP MJX-fixedlimits"> <munder> <mrow> <mn>2</mn> <mo>⋅<!-- ⋅ --></mo> <mi>x</mi> </mrow> <mo>⏟<!-- ⏟ --></mo> </munder> </mrow> <mrow class="MJX-TeXAtom-ORD"> <mstyle mathcolor="#58429B"> <mrow class="MJX-TeXAtom-ORD"> <mtext>output expression</mtext> </mrow> </mstyle> </mrow> </munder> <mo>∣<!-- ∣ --></mo> <munder> <mrow class="MJX-TeXAtom-OP MJX-fixedlimits"> <munder> <mi>x</mi> <mo>⏟<!-- ⏟ --></mo> </munder> </mrow> <mrow class="MJX-TeXAtom-ORD"> <mstyle mathcolor="#58429B"> <mrow class="MJX-TeXAtom-ORD"> <mtext>variable</mtext> </mrow> </mstyle> </mrow> </munder> <mo>∈<!-- ∈ --></mo> <munder> <mrow class="MJX-TeXAtom-OP MJX-fixedlimits"> <munder> <mrow class="MJX-TeXAtom-ORD"> <mi mathvariant="double-struck">N</mi> </mrow> <mo>⏟<!-- ⏟ --></mo> </munder> </mrow> <mrow class="MJX-TeXAtom-ORD"> <mstyle mathcolor="#58429B"> <mrow class="MJX-TeXAtom-ORD"> <mtext>input set</mtext> </mrow> </mstyle> </mrow> </munder> <mo>,</mo> <mtext> </mtext> <munder> <mrow class="MJX-TeXAtom-OP MJX-fixedlimits"> <munder> <mrow> <msup> <mi>x</mi> <mrow class="MJX-TeXAtom-ORD"> <mn>2</mn> </mrow> </msup> <mo>></mo> <mn>3</mn> </mrow> <mo>⏟<!-- ⏟ --></mo> </munder> </mrow> <mrow class="MJX-TeXAtom-ORD"> <mstyle mathcolor="#58429B"> <mrow class="MJX-TeXAtom-ORD"> <mtext>predicate</mtext> </mrow> </mstyle> </mrow> </munder> <mo fence="false" stretchy="false">}</mo> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle S=\{\underbrace {2\cdot x} _{\color {Violet}{\text{output expression}}}\mid \underbrace {x} _{\color {Violet}{\text{variable}}}\in \underbrace {\mathbb {N} } _{\color {Violet}{\text{input set}}},\ \underbrace {x^{2}>3} _{\color {Violet}{\text{predicate}}}\}}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/611bbc7dd2005e4d52e287cdbf66cfb90782ccdb" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -4.171ex; width:44.912ex; height:6.509ex;" alt="{\displaystyle S=\{\underbrace {2\cdot x} _{\color {Violet}{\text{output expression}}}\mid \underbrace {x} _{\color {Violet}{\text{variable}}}\in \underbrace {\mathbb {N} } _{\color {Violet}{\text{input set}}},\ \underbrace {x^{2}>3} _{\color {Violet}{\text{predicate}}}\}}"></span></dd></dl> <ul><li><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mi>x</mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle x}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/87f9e315fd7e2ba406057a97300593c4802b53e4" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}"></span> is the variable representing members of an input set.</li> <li><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \mathbb {N} }"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mrow class="MJX-TeXAtom-ORD"> <mi mathvariant="double-struck">N</mi> </mrow> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle \mathbb {N} }</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/fdf9a96b565ea202d0f4322e9195613fb26a9bed" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.678ex; height:2.176ex;" alt="{\displaystyle \mathbb {N} }"></span> represents the input set, which in this example is the set of natural numbers</li> <li><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x^{2}>3}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <msup> <mi>x</mi> <mrow class="MJX-TeXAtom-ORD"> <mn>2</mn> </mrow> </msup> <mo>></mo> <mn>3</mn> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle x^{2}>3}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/66f5674402c8d90fd29f829d89d8ccc67efba4be" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:6.645ex; height:2.676ex;" alt="{\displaystyle x^{2}>3}"></span> is a <a href="/wiki/Predicate_(logic)" class="mw-redirect" title="Predicate (logic)">predicate</a> expression acting as a filter on members of the input set.</li> <li><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2\cdot x}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mn>2</mn> <mo>⋅<!-- ⋅ --></mo> <mi>x</mi> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle 2\cdot x}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/31ff6145eb06b9724034b07d945719dd7fb50c0a" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:4.171ex; height:2.176ex;" alt="{\displaystyle 2\cdot x}"></span> is an output expression producing members of the new set from members of the input set that satisfy the predicate expression.</li> <li><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \{\}}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo fence="false" stretchy="false">{</mo> <mo fence="false" stretchy="false">}</mo> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle \{\}}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/3e6f1caa524dfcc90158ad69a51b5f9577fe5f1f" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:2.325ex; height:2.843ex;" alt="{\displaystyle \{\}}"></span> braces indicate that the result is a set</li> <li><span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \mid }"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo>∣<!-- ∣ --></mo> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle \mid }</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/8f7b2136e276c4aec285a6c40b91180c16432b9b" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:0.647ex; height:2.843ex;" alt="{\displaystyle \mid }"></span> <span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle ,}"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mo>,</mo> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle ,}</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/b8fa4cba3a446de313920e16251756e27312b825" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:0.647ex; height:1.176ex;" alt="{\displaystyle ,}"></span> the vertical bar is read as "SUCH THAT". The bar and the colon ":" are used interchangeably.</li> <li>commas separate the predicates and can be read as "AND".</li></ul> <p>A list comprehension has the same syntactic components to represent generation of a list in order from an input <a href="/wiki/List_(computing)" class="mw-redirect" title="List (computing)">list</a> or <a href="/wiki/Iterator" title="Iterator">iterator</a>: </p> <ul><li>A variable representing members of an input list.</li> <li>An input list (or iterator).</li> <li>An optional predicate expression.</li> <li>And an output expression producing members of the output list from members of the input iterable that satisfy the predicate.</li></ul> <p>The order of generation of members of the output list is based on the order of items in the input. </p><p>In <a href="/wiki/Haskell_(programming_language)" class="mw-redirect" title="Haskell (programming language)">Haskell's</a> list comprehension syntax, this set-builder construct would be written similarly, as: </p> <div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span></span><span class="nf">s</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="o">..</span><span class="p">],</span><span class="w"> </span><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">]</span> </pre></div> <p>Here, the list <code>[0..]</code> represents <span class="mwe-math-element"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \mathbb {N} }"> <semantics> <mrow class="MJX-TeXAtom-ORD"> <mstyle displaystyle="true" scriptlevel="0"> <mrow class="MJX-TeXAtom-ORD"> <mi mathvariant="double-struck">N</mi> </mrow> </mstyle> </mrow> <annotation encoding="application/x-tex">{\displaystyle \mathbb {N} }</annotation> </semantics> </math></span><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/fdf9a96b565ea202d0f4322e9195613fb26a9bed" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.678ex; height:2.176ex;" alt="{\displaystyle \mathbb {N} }"></span>, <code>x^2>3</code> represents the predicate, and <code>2*x</code> represents the output expression. </p><p>List comprehensions give results in a defined order (unlike the members of sets); and list comprehensions may <a href="/wiki/Generator_(computer_science)" class="mw-redirect" title="Generator (computer science)">generate</a> the members of a list in order, rather than produce the entirety of the list thus allowing, for example, the previous Haskell definition of the members of an infinite list. </p> <div class="mw-heading mw-heading2"><h2 id="History">History</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=2" title="Edit section: History"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The existence of related constructs predates the use of the term "List Comprehension". The <a href="/wiki/SETL" title="SETL">SETL</a> programming language (1969) has a set formation construct which is similar to list comprehensions. E.g., this code prints all prime numbers from 2 to <var style="padding-right: 1px;">N</var>: </p> <pre>print([n in [2..N] | forall m in {2..n - 1} | n mod m > 0]); </pre> <p>The <a href="/wiki/Computer_algebra_system" title="Computer algebra system">computer algebra system</a> <a href="/wiki/Axiom_(computer_algebra_system)" title="Axiom (computer algebra system)">AXIOM</a> (1973) has a similar construct that processes <a href="/wiki/Stream_(computing)" title="Stream (computing)">streams</a>. </p><p>The first use of the term "comprehension" for such constructs was in <a href="/wiki/Rod_Burstall" title="Rod Burstall">Rod Burstall</a> and <a href="/wiki/John_Darlington" title="John Darlington">John Darlington</a>'s description of their functional programming language <a href="/wiki/NPL_programming_language" class="mw-redirect" title="NPL programming language">NPL</a> from 1977. In his retrospective "Some History of Functional Programming Languages",<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> <a href="/wiki/David_Turner_(computer_scientist)" title="David Turner (computer scientist)">David Turner</a> recalls: </p> <style data-mw-deduplicate="TemplateStyles:r1244412712">.mw-parser-output .templatequote{overflow:hidden;margin:1em 0;padding:0 32px}.mw-parser-output .templatequotecite{line-height:1.5em;text-align:left;margin-top:0}@media(min-width:500px){.mw-parser-output .templatequotecite{padding-left:1.6em}}</style><blockquote class="templatequote"><p>NPL was implemented in POP2 by Burstall and used for Darlington’s work on program transformation (Burstall & Darlington 1977). The language was first order, strongly (but not polymorphically) typed, purely functional, call-by-value. It also had “set expressions” e.g. </p><dl><dd><pre>setofeven (X) <= <:x : x in X & even(x):>}}</pre></dd></dl></blockquote> <p>In a footnote attached to the term "list comprehension", Turner also notes </p> <link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1244412712"><blockquote class="templatequote"><p>I initially called these <i>ZF expressions</i>, a reference to <a href="/wiki/Zermelo%E2%80%93Fraenkel_set_theory" title="Zermelo–Fraenkel set theory">Zermelo–Fraenkel set theory</a> — it was <a href="/wiki/Philip_Wadler" title="Philip Wadler">Phil Wadler</a> who coined the better term <i>list comprehension</i>.</p></blockquote> <p>Burstall and Darlington's work with NPL influenced many functional programming languages during the 1980s, but not all included list comprehensions. An exception was Turner's influential, pure, lazy, functional programming language <a href="/wiki/Miranda_programming_language" class="mw-redirect" title="Miranda programming language">Miranda</a>, released in 1985. The subsequently developed standard pure lazy functional language <a href="/wiki/Haskell_programming_language" class="mw-redirect" title="Haskell programming language">Haskell</a> includes many of Miranda's features, including list comprehensions. </p><p>Comprehensions were proposed as a query notation for databases<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> and were implemented in the <i><a href="/wiki/Heinrich_Kleisli" title="Heinrich Kleisli">Kleisli</a></i> database query language.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> </p> <div class="mw-heading mw-heading2"><h2 id="Examples_in_different_programming_languages">Examples in different programming languages</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=3" title="Edit section: Examples in different programming languages"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <style data-mw-deduplicate="TemplateStyles:r1236090951">.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="/wiki/Comparison_of_programming_languages_(list_comprehension)" title="Comparison of programming languages (list comprehension)">Comparison of programming languages (list comprehension)</a></div><div class="mw-highlight mw-highlight-lang-julia mw-content-ltr" dir="ltr"></div> <div class="mw-heading mw-heading2"><h2 id="Similar_constructs">Similar constructs</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=4" title="Edit section: Similar constructs"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <div class="mw-heading mw-heading3"><h3 id="Monad_comprehension">Monad comprehension</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=5" title="Edit section: Monad comprehension"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>In Haskell, a <a href="/wiki/Monads_in_functional_programming#do-notation" class="mw-redirect" title="Monads in functional programming">monad comprehension</a> is a generalization of the list comprehension to other <a href="/wiki/Monads_in_functional_programming" class="mw-redirect" title="Monads in functional programming">monads in functional programming</a>. </p> <div class="mw-heading mw-heading3"><h3 id="Set_comprehension">Set comprehension</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=6" title="Edit section: Set comprehension"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The <a href="/wiki/Python_(programming_language)" title="Python (programming language)">Python</a> language introduces syntax for <a href="/wiki/Set_(computer_science)" class="mw-redirect" title="Set (computer science)">set</a> comprehensions starting in version 2.7. Similar in form to list comprehensions, set comprehensions generate Python sets instead of lists. </p> <div class="mw-highlight mw-highlight-lang-pycon mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="p">{</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="s1">'ABCDABCD'</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="s1">'CB'</span><span class="p">}</span> <span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="go">{'A', 'D'}</span> <span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="go"><class 'set'></span> <span class="gp">>>></span> </pre></div> <p><a href="/wiki/Racket_(programming_language)" title="Racket (programming language)">Racket</a> set comprehensions generate Racket sets instead of lists. </p> <div class="mw-highlight mw-highlight-lang-scheme mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="nf">for/set</span><span class="w"> </span><span class="p">([</span><span class="nv">v</span><span class="w"> </span><span class="s">"ABCDABCD"</span><span class="p">]</span><span class="w"> </span><span class="kd">#:unless</span><span class="w"> </span><span class="p">(</span><span class="nb">member</span><span class="w"> </span><span class="nv">v</span><span class="w"> </span><span class="p">(</span><span class="nb">string->list</span><span class="w"> </span><span class="s">"CB"</span><span class="p">)))</span> <span class="w"> </span><span class="nv">v</span><span class="p">))</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Dictionary_comprehension">Dictionary comprehension</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=7" title="Edit section: Dictionary comprehension"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The <a href="/wiki/Python_(programming_language)" title="Python (programming language)">Python</a> language introduced a new syntax for <a href="/wiki/Associative_array" title="Associative array">dictionary</a> comprehensions in version 2.7, similar in form to list comprehensions but which generate Python <a rel="nofollow" class="external text" href="https://docs.python.org/library/stdtypes.html#dict">dicts</a> instead of lists. </p> <div class="mw-highlight mw-highlight-lang-pycon mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">val</span> <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="s1">'ABCD'</span><span class="p">)</span> <span class="k">if</span> <span class="n">val</span> <span class="ow">not</span> <span class="ow">in</span> <span class="s1">'CB'</span><span class="p">}</span> <span class="gp">>>> </span><span class="n">s</span> <span class="go">{0: 'A', 3: 'D'}</span> <span class="gp">>>></span> </pre></div> <p>Racket hash table comprehensions generate Racket hash tables (one implementation of the Racket dictionary type). </p> <div class="mw-highlight mw-highlight-lang-scheme mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="nf">for/hash</span><span class="w"> </span><span class="p">([(</span><span class="nf">val</span><span class="w"> </span><span class="nv">key</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nf">in-indexed</span><span class="w"> </span><span class="s">"ABCD"</span><span class="p">)]</span> <span class="w"> </span><span class="kd">#:unless</span><span class="w"> </span><span class="p">(</span><span class="nb">member</span><span class="w"> </span><span class="nv">val</span><span class="w"> </span><span class="p">(</span><span class="nb">string->list</span><span class="w"> </span><span class="s">"CB"</span><span class="p">)))</span> <span class="w"> </span><span class="p">(</span><span class="nb">values</span><span class="w"> </span><span class="nv">key</span><span class="w"> </span><span class="nv">val</span><span class="p">))</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="Parallel_list_comprehension">Parallel list comprehension</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=8" title="Edit section: Parallel list comprehension"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>The <a href="/wiki/Glasgow_Haskell_Compiler" title="Glasgow Haskell Compiler">Glasgow Haskell Compiler</a> has an extension called <b>parallel list comprehension</b> (also known as <b>zip-comprehension</b>) that permits multiple independent branches of qualifiers within the list comprehension syntax. Whereas qualifiers separated by commas are dependent ("nested"), qualifier branches separated by pipes are evaluated in parallel (this does not refer to any form of multithreadedness: it merely means that the branches are <a href="/wiki/Map_(higher-order_function)" title="Map (higher-order function)">zipped</a>). </p> <div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">-- regular list comprehension</span> <span class="nf">a</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="p">[(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">],</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="p">[</span><span class="mi">3</span><span class="o">..</span><span class="mi">5</span><span class="p">]]</span> <span class="c1">-- [(1,3),(1,4),(1,5),(2,3),(2,4) ...</span> <span class="c1">-- zipped list comprehension</span> <span class="nf">b</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="p">[(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="n">zip</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">3</span><span class="o">..</span><span class="mi">5</span><span class="p">]]</span> <span class="c1">-- [(1,3),(2,4),(3,5)]</span> <span class="c1">-- parallel list comprehension</span> <span class="nf">c</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="p">[(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">]</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="p">[</span><span class="mi">3</span><span class="o">..</span><span class="mi">5</span><span class="p">]]</span> <span class="c1">-- [(1,3),(2,4),(3,5)]</span> </pre></div> <p>Racket's comprehensions standard library contains parallel and nested versions of its comprehensions, distinguished by "for" vs "for*" in the name. For example, the vector comprehensions "for/vector" and "for*/vector" create vectors by parallel versus nested iteration over sequences. The following is Racket code for the Haskell list comprehension examples. </p> <div class="mw-highlight mw-highlight-lang-scheme mw-content-ltr" dir="ltr"><pre><span></span><span class="nb">></span><span class="w"> </span><span class="p">(</span><span class="nf">for*/list</span><span class="w"> </span><span class="p">([</span><span class="nv">x</span><span class="w"> </span><span class="p">(</span><span class="nf">in-range</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">6</span><span class="p">)]</span><span class="w"> </span><span class="p">[</span><span class="nv">y</span><span class="w"> </span><span class="p">(</span><span class="nf">in-range</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">6</span><span class="p">)])</span><span class="w"> </span><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="nv">y</span><span class="p">))</span> <span class="o">'</span><span class="p">((</span><span class="mi">1</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">4</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">4</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">5</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">5</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">5</span><span class="w"> </span><span class="mi">5</span><span class="p">))</span> <span class="nb">></span><span class="w"> </span><span class="p">(</span><span class="nf">for/list</span><span class="w"> </span><span class="p">([</span><span class="nv">x</span><span class="w"> </span><span class="p">(</span><span class="nf">in-range</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">6</span><span class="p">)]</span><span class="w"> </span><span class="p">[</span><span class="nv">y</span><span class="w"> </span><span class="p">(</span><span class="nf">in-range</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">6</span><span class="p">)])</span><span class="w"> </span><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="nv">y</span><span class="p">))</span> <span class="o">'</span><span class="p">((</span><span class="mi">1</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="w"> </span><span class="mi">5</span><span class="p">))</span> </pre></div> <p>In Python, we could do as follows: </p> <div class="mw-highlight mw-highlight-lang-pycon mw-content-ltr" dir="ltr"><pre><span></span><span class="gp">>>> </span><span class="c1"># regular list comprehension</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">)]</span> <span class="go">[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), ...</span> <span class="gp">>>></span> <span class="gp">>>> </span><span class="c1"># parallel/zipped list comprehension</span> <span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">))]</span> <span class="go">[(1, 3), (2, 4), (3, 5)]</span> </pre></div> <p>In Julia, practically the same results can be achieved as follows: </p> <div class="mw-highlight mw-highlight-lang-julia mw-content-ltr" dir="ltr"><pre><span></span><span class="c"># regular array comprehension</span> <span class="o">>>></span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="mi">5</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="mi">5</span><span class="p">]</span> <span class="c"># parallel/zipped array comprehension</span> <span class="o">>>></span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">zip</span><span class="p">(</span><span class="mi">1</span><span class="o">:</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="mi">5</span><span class="p">)]</span> </pre></div> <p>with the only difference that instead of lists, in Julia, we have arrays. </p> <div class="mw-heading mw-heading3"><h3 id="XQuery_and_XPath">XQuery and XPath</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=9" title="Edit section: XQuery and XPath"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>Like the original NPL use, these are fundamentally database access languages. </p><p>This makes the comprehension concept more important, because it is computationally infeasible to retrieve the entire list and operate on it (the initial 'entire list' may be an entire XML database). </p><p>In XPath, the expression: </p> <div class="mw-highlight mw-highlight-lang-xquery mw-content-ltr" dir="ltr"><pre><span></span><span class="p">/</span><span class="nt">library</span><span class="p">/</span><span class="nt">book</span><span class="p">//</span><span class="nt">paragraph</span><span class="p">[</span><span class="na">@style</span><span class="o">=</span><span class="s1">'first-in-chapter'</span><span class="p">]</span> </pre></div> <p>is conceptually evaluated as a series of "steps" where each step produces a list and the next step applies a filter function to each element in the previous step's output.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> </p><p>In XQuery, full XPath is available, but <a href="/wiki/FLWOR" title="FLWOR">FLWOR</a> statements are also used, which is a more powerful comprehension construct.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> </p> <div class="mw-highlight mw-highlight-lang-xquery mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="nv">$</span><span class="n">b</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="p">//</span><span class="nt">book</span> <span class="k">where</span><span class="w"> </span><span class="nv">$</span><span class="n">b</span><span class="p">[</span><span class="na">@pages</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">400</span><span class="p">]</span> <span class="k">order by</span><span class="w"> </span><span class="nv">$</span><span class="n">b</span><span class="p">//</span><span class="nt">title</span> <span class="k">return</span> <span class="w"> </span><span class="nt"><shortBook></span> <span class="l"> </span><span class="nt"><title></span><span class="p">{</span><span class="nv">$</span><span class="n">b</span><span class="p">//</span><span class="nt">title</span><span class="p">}</span><span class="nt"></title></span> <span class="l"> </span><span class="nt"><firstPara></span><span class="p">{(</span><span class="nv">$</span><span class="n">book</span><span class="p">//</span><span class="nt">paragraph</span><span class="p">)[</span><span class="mi">1</span><span class="p">]}</span><span class="nt"></firstPara></span> <span class="l"> </span><span class="nt"></shortBook></span> </pre></div> <p>Here the XPath //book is evaluated to create a sequence (aka list); the where clause is a functional "filter", the order by sorts the result, and the <code class="nowrap" style=""><shortBook>...</shortBook></code> XML snippet is actually an <a href="/wiki/Anonymous_function" title="Anonymous function">anonymous function</a> that builds/transforms XML for each element in the sequence using the 'map' approach found in other functional languages. </p><p>So, in another functional language the above FLWOR statement may be implemented like this: </p> <div class="mw-highlight mw-highlight-lang-xquery mw-content-ltr" dir="ltr"><pre><span></span><span class="nf">map</span><span class="p">(</span> <span class="w"> </span><span class="nf">newXML</span><span class="p">(</span><span class="nt">shortBook</span><span class="p">,</span><span class="w"> </span><span class="nf">newXML</span><span class="p">(</span><span class="nt">title</span><span class="p">,</span><span class="w"> </span><span class="nv">$</span><span class="err">1.</span><span class="n">title</span><span class="p">),</span><span class="w"> </span><span class="nf">newXML</span><span class="p">(</span><span class="nt">firstPara</span><span class="p">,</span><span class="w"> </span><span class="nv">$</span><span class="err">1...))</span> <span class="w"> </span><span class="nf">filter</span><span class="p">(</span> <span class="w"> </span><span class="nf">lt</span><span class="p">(</span><span class="nv">$</span><span class="err">1.</span><span class="n">pages</span><span class="p">,</span><span class="w"> </span><span class="mi">400</span><span class="p">),</span> <span class="w"> </span><span class="nf">xpath</span><span class="p">(//</span><span class="nt">book</span><span class="p">)</span> <span class="w"> </span><span class="p">)</span> <span class="p">)</span> </pre></div> <div class="mw-heading mw-heading3"><h3 id="LINQ_in_C#"><span id="LINQ_in_C.23"></span>LINQ in C#</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=10" title="Edit section: LINQ in C#"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p><a href="/wiki/C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> 3.0 has a group of related features called <a href="/wiki/Language_Integrated_Query" title="Language Integrated Query">LINQ</a>, which defines a set of query operators for manipulating object enumerations. </p> <div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">var</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Enumerable</span><span class="p">.</span><span class="n">Range</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">100</span><span class="p">).</span><span class="n">Where</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">3</span><span class="p">).</span><span class="n">Select</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">2</span><span class="p">);</span> </pre></div> <p>It also offers an alternative comprehension syntax, reminiscent of SQL: </p> <div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">var</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">from</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">Enumerable</span><span class="p">.</span><span class="n">Range</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">100</span><span class="p">)</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">3</span><span class="w"> </span><span class="k">select</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="m">2</span><span class="p">;</span> </pre></div> <p>LINQ provides a capability over typical list comprehension implementations. When the root object of the comprehension implements the <code>IQueryable</code> interface, rather than just executing the chained methods of the comprehension, the entire sequence of commands are converted into an <a href="/wiki/Abstract_syntax_tree" title="Abstract syntax tree">abstract syntax tree</a> (AST) object, which is passed to the IQueryable object to interpret and execute. </p><p>This allows, amongst other things, for the IQueryable to </p> <ul><li>rewrite an incompatible or inefficient comprehension</li> <li>translate the AST into another query language (e.g. SQL) for execution</li></ul> <div class="mw-heading mw-heading3"><h3 id="C++"><span id="C.2B.2B"></span>C++</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=11" title="Edit section: C++"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>C++ does not have any language features directly supporting list comprehensions but <a href="/wiki/Operator_overloading" title="Operator overloading">operator overloading</a> (e.g., overloading <code>|</code>, <code>>></code>, <code>>>=</code>) has been used successfully to provide expressive syntax for "embedded" query <a href="/wiki/Domain-specific_language" title="Domain-specific language">domain-specific languages</a> (DSL). Alternatively, list comprehensions can be constructed using the <a href="/wiki/Erase-remove_idiom" class="mw-redirect" title="Erase-remove idiom">erase-remove idiom</a> to select elements in a container and the STL algorithm for_each to transform them. </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf"><algorithm></span> <span class="cp">#include</span><span class="w"> </span><span class="cpf"><list></span> <span class="cp">#include</span><span class="w"> </span><span class="cpf"><numeric></span> <span class="k">using</span><span class="w"> </span><span class="k">namespace</span><span class="w"> </span><span class="nn">std</span><span class="p">;</span> <span class="k">template</span><span class="o"><</span><span class="k">class</span><span class="w"> </span><span class="nc">C</span><span class="p">,</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">P</span><span class="p">,</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">T</span><span class="o">></span> <span class="n">C</span><span class="w"> </span><span class="n">comprehend</span><span class="p">(</span><span class="n">C</span><span class="o">&&</span><span class="w"> </span><span class="n">source</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">P</span><span class="o">&</span><span class="w"> </span><span class="n">predicate</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="o">&</span><span class="w"> </span><span class="n">transformation</span><span class="p">)</span> <span class="p">{</span> <span class="w"> </span><span class="c1">// initialize destination</span> <span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">forward</span><span class="o"><</span><span class="n">C</span><span class="o">></span><span class="p">(</span><span class="n">source</span><span class="p">);</span> <span class="w"> </span><span class="c1">// filter elements</span> <span class="w"> </span><span class="n">d</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="n">remove_if</span><span class="p">(</span><span class="n">begin</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="w"> </span><span class="n">end</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="w"> </span><span class="n">predicate</span><span class="p">),</span><span class="w"> </span><span class="n">end</span><span class="p">(</span><span class="n">d</span><span class="p">));</span> <span class="w"> </span><span class="c1">// apply transformation</span> <span class="w"> </span><span class="n">for_each</span><span class="p">(</span><span class="n">begin</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="w"> </span><span class="n">end</span><span class="p">(</span><span class="n">d</span><span class="p">),</span><span class="w"> </span><span class="n">transformation</span><span class="p">);</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">d</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span><span class="w"> </span><span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="w"> </span><span class="n">list</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">range</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span> <span class="w"> </span><span class="c1">// range is a list of 10 elements, all zero</span> <span class="w"> </span><span class="n">iota</span><span class="p">(</span><span class="n">begin</span><span class="p">(</span><span class="n">range</span><span class="p">),</span><span class="w"> </span><span class="n">end</span><span class="p">(</span><span class="n">range</span><span class="p">),</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span> <span class="w"> </span><span class="c1">// range now contains 1, 2, ..., 10</span> <span class="w"> </span><span class="n">list</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">comprehend</span><span class="p">(</span> <span class="w"> </span><span class="n">range</span><span class="p">,</span> <span class="w"> </span><span class="p">[](</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">},</span> <span class="w"> </span><span class="p">[](</span><span class="kt">int</span><span class="w"> </span><span class="o">&</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">});</span> <span class="w"> </span><span class="c1">// result now contains 4, 6, ..., 20</span> <span class="p">}</span> </pre></div> <p>There is some effort in providing C++ with list-comprehension constructs/syntax similar to the set builder notation. </p> <ul><li>In <a href="/wiki/Boost_C%2B%2B_Libraries" class="mw-redirect" title="Boost C++ Libraries">Boost</a>. Range <a rel="nofollow" class="external autonumber" href="http://www.boost.org/libs/range">[1]</a> library there is a notion of adaptors <a rel="nofollow" class="external autonumber" href="http://www.boost.org/libs/range/doc/html/range/reference/adaptors.html">[2]</a> that can be applied to any range and do filtering, transformation etc. With this library, the original Haskell example would look like (using Boost.Lambda <a rel="nofollow" class="external autonumber" href="http://www.boost.org/libs/lambda">[3]</a> for anonymous filtering and transforming functions) (<a rel="nofollow" class="external text" href="http://codepad.org/y4bpgLJu">Full example</a>):<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="n">counting_range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">filtered</span><span class="p">(</span><span class="w"> </span><span class="n">_1</span><span class="o">*</span><span class="n">_1</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">transformed</span><span class="p">(</span><span class="n">ret</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="p">(</span><span class="w"> </span><span class="n">_1</span><span class="o">*</span><span class="mi">2</span><span class="w"> </span><span class="p">))</span> </pre></div></li> <li>This<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> implementation uses a macro and overloads the << operator. It evaluates any expression valid inside an 'if', and any variable name may be chosen. It's not <a href="/wiki/Thread_safety" title="Thread safety">threadsafe</a>, however. Usage example:</li></ul> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="n">list</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">N</span><span class="p">;</span> <span class="n">list</span><span class="o"><</span><span class="kt">double</span><span class="o">></span><span class="w"> </span><span class="n">S</span><span class="p">;</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="w"> </span><span class="n">N</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> <span class="n">S</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">list_comprehension</span><span class="p">(</span><span class="mf">3.1415</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">3</span><span class="p">)</span> </pre></div> <ul><li>This<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> implementation provides head/tail slicing using classes and operator overloading, and the | operator for filtering lists (using functions). Usage example:</li></ul> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">bool</span><span class="w"> </span><span class="nf">even</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="kt">bool</span><span class="w"> </span><span class="nf">x2</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">&</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">true</span><span class="p">;</span><span class="w"> </span><span class="p">}</span> <span class="n">list</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">;</span> <span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">;</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="w"> </span><span class="n">l</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x2</span><span class="p">;</span> <span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">t</span><span class="p">;</span> <span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">9</span><span class="p">;</span> <span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">even</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x2</span><span class="p">;</span> </pre></div> <ul><li>Language for Embedded Query and Traversal (LEESA<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup>) is an embedded DSL in C++ that implements X-Path-like queries using operator overloading. The queries are executed on richly typed xml trees obtained using xml-to-c++ binding from an XSD. There is absolutely no string encoding. Even the names of the xml tags are classes and therefore, there is no way for typos. If a LEESA expression forms an incorrect path that does not exist in the data model, the C++ compiler will reject the code.<br />Consider a catalog xml.</li></ul> <div class="mw-highlight mw-highlight-lang-xml mw-content-ltr" dir="ltr"><pre><span></span><span class="nt"><catalog></span> <span class="w"> </span><span class="nt"><book></span> <span class="w"> </span><span class="nt"><title></span>Hamlet<span class="nt"></title></span> <span class="w"> </span><span class="nt"><price></span>9.99<span class="nt"></price></span> <span class="w"> </span><span class="nt"><author></span> <span class="w"> </span><span class="nt"><name></span>William<span class="w"> </span>Shakespeare<span class="nt"></name></span> <span class="w"> </span><span class="nt"><country></span>England<span class="nt"></country></span> <span class="w"> </span><span class="nt"></author></span> <span class="w"> </span><span class="nt"></book></span> <span class="w"> </span><span class="nt"><book></span>...<span class="nt"></book></span> ... <span class="nt"></catalog></span> </pre></div> <p>LEESA provides <code>>></code> for XPath's / separator. XPath's // separator that "skips" intermediate nodes in the tree is implemented in LEESA using what's known as Strategic Programming. In the example below, catalog_, book_, author_, and name_ are instances of catalog, book, author, and name classes, respectively. </p> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">// Equivalent X-Path: "catalog/book/author/name"</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="n">name</span><span class="o">></span><span class="w"> </span><span class="n">author_names</span><span class="w"> </span><span class="o">=</span><span class="w"> </span> <span class="n">evaluate</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="w"> </span><span class="n">catalog_</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">book_</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">author_</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">name_</span><span class="p">);</span> <span class="c1">// Equivalent X-Path: "catalog//name"</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="n">name</span><span class="o">></span><span class="w"> </span><span class="n">author_names</span><span class="w"> </span><span class="o">=</span><span class="w"> </span> <span class="n">evaluate</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="w"> </span><span class="n">catalog_</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">DescendantsOf</span><span class="p">(</span><span class="n">catalog_</span><span class="p">,</span><span class="w"> </span><span class="n">name_</span><span class="p">));</span> <span class="c1">// Equivalent X-Path: "catalog//author[country=="England"]"</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="n">name</span><span class="o">></span><span class="w"> </span><span class="n">author_names</span><span class="w"> </span><span class="o">=</span><span class="w"> </span> <span class="n">evaluate</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="w"> </span><span class="n">catalog_</span><span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">DescendantsOf</span><span class="p">(</span><span class="n">catalog_</span><span class="p">,</span><span class="w"> </span><span class="n">author_</span><span class="p">)</span> <span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">Select</span><span class="p">(</span><span class="n">author_</span><span class="p">,</span><span class="w"> </span><span class="p">[](</span><span class="k">const</span><span class="w"> </span><span class="n">author</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">country</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">"England"</span><span class="p">;</span><span class="w"> </span><span class="p">})</span> <span class="w"> </span><span class="o">>></span><span class="w"> </span><span class="n">name_</span><span class="p">);</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=12" title="Edit section: See also"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a href="/wiki/Set-builder_notation" title="Set-builder notation">Set-builder notation</a></li> <li>The <a href="/wiki/Select_(SQL)" title="Select (SQL)">SELECT</a> statement together with its FROM and WHERE clauses in <a href="/wiki/SQL#Queries" title="SQL">SQL</a></li></ul> <div class="mw-heading mw-heading2"><h2 id="Notes_and_references">Notes and references</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=13" title="Edit section: Notes and references"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <style data-mw-deduplicate="TemplateStyles:r1239543626">.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}</style><div class="reflist"> <div class="mw-references-wrap"><ol class="references"> <li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/d/d6/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/a/aa/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("//upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}</style><cite id="CITEREFTurner2012" class="citation conference cs1">Turner, David (2012). <a rel="nofollow" class="external text" href="https://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf">"Some history of functional programming languages"</a> <span class="cs1-format">(PDF)</span>. <i>International Symposium on Trends in Functional Programming, Springer, Berlin, Heidelberg</i>. pp. <span class="nowrap">1–</span>20.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=conference&rft.atitle=Some+history+of+functional+programming+languages&rft.btitle=International+Symposium+on+Trends+in+Functional+Programming%2C+Springer%2C+Berlin%2C+Heidelberg&rft.pages=%3Cspan+class%3D%22nowrap%22%3E1-%3C%2Fspan%3E20&rft.date=2012&rft.aulast=Turner&rft.aufirst=David&rft_id=https%3A%2F%2Fwww.cs.kent.ac.uk%2Fpeople%2Fstaff%2Fdat%2Ftfp12%2Ftfp12.pdf&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></span> </li> <li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://portal.acm.org/citation.cfm?coll=GUIDE&dl=GUIDE&id=135271">Comprehensions, a query notation for DBPLs</a></span> </li> <li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://portal.acm.org/citation.cfm?id=351241&dl=ACM&coll=portal">The functional guts of the Kleisli query system</a></span> </li> <li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20121209085946/http://www.w3.org/TR/xpath/#section-Location-Steps">"2.1 Location Steps"</a>. <i>XML Path Language (XPath)</i>. <a href="/wiki/W3C" class="mw-redirect" title="W3C">W3C</a>. 16 November 1999. Archived from <a rel="nofollow" class="external text" href="http://www.w3.org/TR/xpath#section-Location-Steps">the original</a> on 9 December 2012<span class="reference-accessdate">. Retrieved <span class="nowrap">24 December</span> 2008</span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=XML+Path+Language+%28XPath%29&rft.atitle=2.1+Location+Steps&rft.date=1999-11-16&rft_id=http%3A%2F%2Fwww.w3.org%2FTR%2Fxpath%23section-Location-Steps&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></span> </li> <li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20111008001258/http://w3schools.com/xquery/xquery_flwor.asp">"XQuery FLWOR Expressions"</a>. <i><a href="/wiki/W3Schools" title="W3Schools">W3Schools</a></i>. Archived from <a rel="nofollow" class="external text" href="https://www.w3schools.com/XQuery/xquery_flwor.asp">the original</a> on 2011-10-08.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=unknown&rft.jtitle=W3Schools&rft.atitle=XQuery+FLWOR+Expressions&rft_id=https%3A%2F%2Fwww.w3schools.com%2FXQuery%2Fxquery_flwor.asp&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></span> </li> <li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20110821211656/http://mfoliveira.org/blog/2011/01/04/simple-list-comprehension-in-cp-using-preprocessor-macros/">"Single-variable List Comprehension in C++ using Preprocessor Macros"</a>. Archived from <a rel="nofollow" class="external text" href="http://mfoliveira.org/blog/2011/01/04/simple-list-comprehension-in-cp-using-preprocessor-macros/">the original</a> on 2011-08-21<span class="reference-accessdate">. Retrieved <span class="nowrap">2011-01-09</span></span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Single-variable+List+Comprehension+in+C%2B%2B+using+Preprocessor+Macros&rft_id=http%3A%2F%2Fmfoliveira.org%2Fblog%2F2011%2F01%2F04%2Fsimple-list-comprehension-in-cp-using-preprocessor-macros%2F&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></span> </li> <li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20170707125836/http://www.tedunangst.com/listcc.html">"C++ list comprehensions"</a>. Archived from <a rel="nofollow" class="external text" href="http://www.tedunangst.com/listcc.html">the original</a> on 2017-07-07<span class="reference-accessdate">. Retrieved <span class="nowrap">2011-01-09</span></span>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=C%2B%2B+list+comprehensions&rft_id=http%3A%2F%2Fwww.tedunangst.com%2Flistcc.html&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></span> </li> <li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.dre.vanderbilt.edu/LEESA/">"Language for Embedded Query and Traversal (LEESA)"</a>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=unknown&rft.btitle=Language+for+Embedded+Query+and+Traversal+%28LEESA%29&rft_id=http%3A%2F%2Fwww.dre.vanderbilt.edu%2FLEESA%2F&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></span> </li> </ol></div></div> <ul><li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20050125080818/http://ftp.sunet.se/foldoc/foldoc.cgi?list+comprehension">List Comprehension</a> in The Free On-line Dictionary of Computing, Editor Denis Howe.</li> <li><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r1238218222"><cite id="CITEREFWadler1990" class="citation conference cs1">Wadler, Philip (1990). <a rel="nofollow" class="external text" href="http://citeseer.ist.psu.edu/wadler92comprehending.html">"Comprehending Monads"</a>. <i>Proceedings of the 1990 ACM Conference on LISP and Functional Programming, Nice</i>.</cite><span title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=conference&rft.atitle=Comprehending+Monads&rft.btitle=Proceedings+of+the+1990+ACM+Conference+on+LISP+and+Functional+Programming%2C+Nice&rft.date=1990&rft.aulast=Wadler&rft.aufirst=Philip&rft_id=http%3A%2F%2Fciteseer.ist.psu.edu%2Fwadler92comprehending.html&rfr_id=info%3Asid%2Fen.wikipedia.org%3AList+comprehension" class="Z3988"></span></li></ul> <div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=14" title="Edit section: External links"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li>SQL-like set operations with list comprehension one-liners in the <a rel="nofollow" class="external text" href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/159974">Python Cookbook</a></li> <li><a rel="nofollow" class="external text" href="http://lambda-the-ultimate.org/classic/message11326.html">Discussion on list comprehensions in Scheme and related constructs</a></li> <li><a rel="nofollow" class="external text" href="http://langexplr.blogspot.com/2007/02/list-comprehensions-across-languages_18.html">List Comprehensions across languages</a></li></ul> <div class="mw-heading mw-heading3"><h3 id="Axiom">Axiom</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=15" title="Edit section: Axiom"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20051018040438/http://page.axiom-developer.org/zope/mathaction/Streams">Axiom stream examples</a></li></ul> <div class="mw-heading mw-heading3"><h3 id="Clojure">Clojure</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=16" title="Edit section: Clojure"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a rel="nofollow" class="external text" href="https://clojuredocs.org/clojure.core/for">Clojure API documentation - <i>for</i> macro</a></li></ul> <div class="mw-heading mw-heading3"><h3 id="Common_Lisp">Common Lisp</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=17" title="Edit section: Common Lisp"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a rel="nofollow" class="external text" href="http://rali.iro.umontreal.ca/rali/?q=en/node/886">Implementation of a Lisp comprehension macro</a> by Guy Lapalme</li></ul> <div class="mw-heading mw-heading3"><h3 id="Haskell">Haskell</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=18" title="Edit section: Haskell"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li>The Haskell 98 Report, chapter <a rel="nofollow" class="external text" href="http://haskell.org/onlinereport/exps.html#list-comprehensions">3.11 List Comprehensions</a>.</li> <li>The Glorious Glasgow Haskell Compilation System User's Guide, chapter <a rel="nofollow" class="external text" href="https://web.archive.org/web/20051129140339/http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#parallel-list-comprehensions">7.3.4 Parallel List Comprehensions</a>.</li> <li>The Hugs 98 User's Guide, chapter <a rel="nofollow" class="external text" href="https://web.archive.org/web/20140515114545/http://cvs.haskell.org/Hugs/pages/users_guide/hugs-ghc.html#ZIP-COMPREHENSION">5.1.2 Parallel list comprehensions (a.k.a. zip-comprehensions)</a>.</li></ul> <div class="mw-heading mw-heading3"><h3 id="OCaml">OCaml</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=19" title="Edit section: OCaml"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li><a rel="nofollow" class="external text" href="http://batteries.forge.ocamlcore.org/">OCaml Batteries Included</a></li> <li><a rel="nofollow" class="external text" href="http://batteries.forge.ocamlcore.org/doc.preview:batteries-alpha3/html/extensions.html">Language extensions introduced in OCaml Batteries Included</a></li></ul> <div class="mw-heading mw-heading3"><h3 id="Python">Python</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=List_comprehension&action=edit&section=20" title="Edit section: Python"><span>edit</span></a><span class="mw-editsection-bracket">]</span></span></div> <ul><li>The Python Tutorial, <a rel="nofollow" class="external text" href="https://docs.python.org/tutorial/datastructures.html#list-comprehensions">List Comprehensions</a>.</li> <li>Python Language Reference, <a rel="nofollow" class="external text" href="https://docs.python.org/reference/expressions.html#list-displays">List displays</a>.</li> <li>Python Enhancement Proposal <a rel="nofollow" class="external text" href="https://www.python.org/peps/pep-0202.html">PEP 202: List Comprehensions</a>.</li> <li>Python Language Reference, <a rel="nofollow" class="external text" href="https://docs.python.org/reference/expressions.html#generator-expressions">Generator expressions</a>.</li> <li>Python Enhancement Proposal <a rel="nofollow" class="external text" href="https://python.org/peps/pep-0289.html">PEP 289: Generator Expressions</a>.</li></ul> <!-- NewPP limit report Parsed by mw‐web.codfw.main‐84b999ff94‐9sl6r Cached time: 20250204091723 Cache expiry: 2592000 Reduced expiry: false Complications: [vary‐revision‐sha1, show‐toc] CPU time usage: 0.285 seconds Real time usage: 0.476 seconds Preprocessor visited node count: 1065/1000000 Post‐expand include size: 13373/2097152 bytes Template argument size: 1453/2097152 bytes Highest expansion depth: 8/100 Expensive parser function count: 22/500 Unstrip recursion depth: 1/20 Unstrip post‐expand size: 65074/5000000 bytes Lua time usage: 0.127/10.000 seconds Lua memory usage: 4459291/52428800 bytes Number of Wikibase entities loaded: 0/400 --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 352.000 1 -total 27.61% 97.201 1 Template:Reflist 24.31% 85.582 1 Template:Short_description 19.81% 69.734 2 Template:Cite_conference 17.30% 60.889 2 Template:Pagetype 6.49% 22.841 1 Template:Main 6.17% 21.720 2 Template:Quote 5.00% 17.613 5 Template:Cite_web 4.13% 14.541 5 Template:Main_other 3.51% 12.368 1 Template:SDcat --> <!-- Saved in parser cache with key enwiki:pcache:275744:|#|:idhash:canonical and timestamp 20250204091723 and revision id 1228787071. Rendering was triggered because: page-view --> </div><!--esi <esi:include src="/esitest-fa8a495983347898/content" /> --><noscript><img src="https://login.wikimedia.org/wiki/Special:CentralAutoLogin/start?useformat=desktop&type=1x1&usesul3=0" alt="" width="1" height="1" style="border: none; position: absolute;"></noscript> <div class="printfooter" data-nosnippet="">Retrieved from "<a dir="ltr" href="https://en.wikipedia.org/w/index.php?title=List_comprehension&oldid=1228787071">https://en.wikipedia.org/w/index.php?title=List_comprehension&oldid=1228787071</a>"</div></div> <div id="catlinks" class="catlinks" data-mw="interface"><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="/wiki/Help:Category" title="Help:Category">Category</a>: <ul><li><a href="/wiki/Category:Programming_constructs" title="Category:Programming constructs">Programming constructs</a></li></ul></div><div id="mw-hidden-catlinks" class="mw-hidden-catlinks mw-hidden-cats-hidden">Hidden categories: <ul><li><a href="/wiki/Category:Articles_with_short_description" title="Category:Articles with short description">Articles with short description</a></li><li><a href="/wiki/Category:Short_description_is_different_from_Wikidata" title="Category:Short description is different from Wikidata">Short description is different from Wikidata</a></li><li><a href="/wiki/Category:Articles_with_example_code" title="Category:Articles with example code">Articles with example code</a></li><li><a href="/wiki/Category:Articles_with_example_Haskell_code" title="Category:Articles with example Haskell code">Articles with example Haskell code</a></li><li><a href="/wiki/Category:Articles_with_example_Python_(programming_language)_code" title="Category:Articles with example Python (programming language) code">Articles with example Python (programming language) code</a></li><li><a href="/wiki/Category:Articles_with_example_Racket_code" title="Category:Articles with example Racket code">Articles with example Racket code</a></li></ul></div></div> </div> </main> </div> <div class="mw-footer-container"> <footer id="footer" class="mw-footer" > <ul id="footer-info"> <li id="footer-info-lastmod"> This page was last edited on 13 June 2024, at 05:46<span class="anonymous-show"> (UTC)</span>.</li> <li id="footer-info-copyright">Text is available under the <a href="/wiki/Wikipedia:Text_of_the_Creative_Commons_Attribution-ShareAlike_4.0_International_License" title="Wikipedia:Text of the Creative Commons Attribution-ShareAlike 4.0 International License">Creative Commons Attribution-ShareAlike 4.0 License</a>; additional terms may apply. By using this site, you agree to the <a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use" class="extiw" title="foundation:Special:MyLanguage/Policy:Terms of Use">Terms of Use</a> and <a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy" class="extiw" title="foundation:Special:MyLanguage/Policy:Privacy policy">Privacy Policy</a>. Wikipedia® is a registered trademark of the <a rel="nofollow" class="external text" href="https://wikimediafoundation.org/">Wikimedia Foundation, Inc.</a>, a non-profit organization.</li> </ul> <ul id="footer-places"> <li id="footer-places-privacy"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy">Privacy policy</a></li> <li id="footer-places-about"><a href="/wiki/Wikipedia:About">About Wikipedia</a></li> <li id="footer-places-disclaimers"><a href="/wiki/Wikipedia:General_disclaimer">Disclaimers</a></li> <li id="footer-places-contact"><a href="//en.wikipedia.org/wiki/Wikipedia:Contact_us">Contact Wikipedia</a></li> <li id="footer-places-wm-codeofconduct"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct">Code of Conduct</a></li> <li id="footer-places-developers"><a href="https://developer.wikimedia.org">Developers</a></li> <li id="footer-places-statslink"><a href="https://stats.wikimedia.org/#/en.wikipedia.org">Statistics</a></li> <li id="footer-places-cookiestatement"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement">Cookie statement</a></li> <li id="footer-places-mobileview"><a href="//en.m.wikipedia.org/w/index.php?title=List_comprehension&mobileaction=toggle_view_mobile" class="noprint stopMobileRedirectToggle">Mobile view</a></li> </ul> <ul id="footer-icons" class="noprint"> <li id="footer-copyrightico"><a href="https://wikimediafoundation.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/static/images/footer/wikimedia-button.svg" width="84" height="29" alt="Wikimedia Foundation" lang="en" loading="lazy"></a></li> <li id="footer-poweredbyico"><a href="https://www.mediawiki.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><picture><source media="(min-width: 500px)" srcset="/w/resources/assets/poweredby_mediawiki.svg" width="88" height="31"><img src="/w/resources/assets/mediawiki_compact.svg" alt="Powered by MediaWiki" width="25" height="25" loading="lazy"></picture></a></li> </ul> </footer> </div> </div> </div> <div class="vector-header-container vector-sticky-header-container"> <div id="vector-sticky-header" class="vector-sticky-header"> <div class="vector-sticky-header-start"> <div class="vector-sticky-header-icon-start vector-button-flush-left vector-button-flush-right" aria-hidden="true"> <button class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-sticky-header-search-toggle" tabindex="-1" data-event-name="ui.vector-sticky-search-form.icon"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>Search</span> </button> </div> <div role="search" class="vector-search-box-vue vector-search-box-show-thumbnail vector-search-box"> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail"> <form action="/w/index.php" id="vector-sticky-search-form" class="cdx-search-input cdx-search-input--has-end-button"> <div class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="Search Wikipedia"> <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="Special:Search"> </div> <button class="cdx-button cdx-search-input__end-button">Search</button> </form> </div> </div> </div> <div class="vector-sticky-header-context-bar"> <nav aria-label="Contents" class="vector-toc-landmark"> <div id="vector-sticky-header-toc" class="vector-dropdown mw-portlet mw-portlet-sticky-header-toc vector-sticky-header-toc vector-button-flush-left" > <input type="checkbox" id="vector-sticky-header-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-sticky-header-toc" class="vector-dropdown-checkbox " aria-label="Toggle the table of contents" > <label id="vector-sticky-header-toc-label" for="vector-sticky-header-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">Toggle the table of contents</span> </label> <div class="vector-dropdown-content"> <div id="vector-sticky-header-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <div class="vector-sticky-header-context-bar-primary" aria-hidden="true" ><span class="mw-page-title-main">List comprehension</span></div> </div> </div> <div class="vector-sticky-header-end" aria-hidden="true"> <div class="vector-sticky-header-icons"> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only" id="ca-talk-sticky-header" tabindex="-1" data-event-name="talk-sticky-header"><span class="vector-icon mw-ui-icon-speechBubbles mw-ui-icon-wikimedia-speechBubbles"></span> <span></span> </a> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only" id="ca-subject-sticky-header" tabindex="-1" data-event-name="subject-sticky-header"><span class="vector-icon mw-ui-icon-article mw-ui-icon-wikimedia-article"></span> <span></span> </a> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only" id="ca-history-sticky-header" tabindex="-1" data-event-name="history-sticky-header"><span class="vector-icon mw-ui-icon-wikimedia-history mw-ui-icon-wikimedia-wikimedia-history"></span> <span></span> </a> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only mw-watchlink" id="ca-watchstar-sticky-header" tabindex="-1" data-event-name="watch-sticky-header"><span class="vector-icon mw-ui-icon-wikimedia-star mw-ui-icon-wikimedia-wikimedia-star"></span> <span></span> </a> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only" id="ca-edit-sticky-header" tabindex="-1" data-event-name="wikitext-edit-sticky-header"><span class="vector-icon mw-ui-icon-wikimedia-wikiText mw-ui-icon-wikimedia-wikimedia-wikiText"></span> <span></span> </a> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only" id="ca-ve-edit-sticky-header" tabindex="-1" data-event-name="ve-edit-sticky-header"><span class="vector-icon mw-ui-icon-wikimedia-edit mw-ui-icon-wikimedia-wikimedia-edit"></span> <span></span> </a> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only" id="ca-viewsource-sticky-header" tabindex="-1" data-event-name="ve-edit-protected-sticky-header"><span class="vector-icon mw-ui-icon-wikimedia-editLock mw-ui-icon-wikimedia-wikimedia-editLock"></span> <span></span> </a> </div> <div class="vector-sticky-header-buttons"> <button class="cdx-button cdx-button--weight-quiet mw-interlanguage-selector" id="p-lang-btn-sticky-header" tabindex="-1" data-event-name="ui.dropdown-p-lang-btn-sticky-header"><span class="vector-icon mw-ui-icon-wikimedia-language mw-ui-icon-wikimedia-wikimedia-language"></span> <span>14 languages</span> </button> <a href="#" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--action-progressive" id="ca-addsection-sticky-header" tabindex="-1" data-event-name="addsection-sticky-header"><span class="vector-icon mw-ui-icon-speechBubbleAdd-progressive mw-ui-icon-wikimedia-speechBubbleAdd-progressive"></span> <span>Add topic</span> </a> </div> <div class="vector-sticky-header-icon-end"> <div class="vector-user-links"> </div> </div> </div> </div> </div> <div class="vector-settings" id="p-dock-bottom"> <ul></ul> </div><script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgHostname":"mw-web.codfw.main-6b9576b97f-ddl59","wgBackendResponseTime":106,"wgPageParseReport":{"limitreport":{"cputime":"0.285","walltime":"0.476","ppvisitednodes":{"value":1065,"limit":1000000},"postexpandincludesize":{"value":13373,"limit":2097152},"templateargumentsize":{"value":1453,"limit":2097152},"expansiondepth":{"value":8,"limit":100},"expensivefunctioncount":{"value":22,"limit":500},"unstrip-depth":{"value":1,"limit":20},"unstrip-size":{"value":65074,"limit":5000000},"entityaccesscount":{"value":0,"limit":400},"timingprofile":["100.00% 352.000 1 -total"," 27.61% 97.201 1 Template:Reflist"," 24.31% 85.582 1 Template:Short_description"," 19.81% 69.734 2 Template:Cite_conference"," 17.30% 60.889 2 Template:Pagetype"," 6.49% 22.841 1 Template:Main"," 6.17% 21.720 2 Template:Quote"," 5.00% 17.613 5 Template:Cite_web"," 4.13% 14.541 5 Template:Main_other"," 3.51% 12.368 1 Template:SDcat"]},"scribunto":{"limitreport-timeusage":{"value":"0.127","limit":"10.000"},"limitreport-memusage":{"value":4459291,"limit":52428800}},"cachereport":{"origin":"mw-web.codfw.main-84b999ff94-9sl6r","timestamp":"20250204091723","ttl":2592000,"transientcontent":false}}});});</script> <script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"Article","name":"List comprehension","url":"https:\/\/en.wikipedia.org\/wiki\/List_comprehension","sameAs":"http:\/\/www.wikidata.org\/entity\/Q795065","mainEntity":"http:\/\/www.wikidata.org\/entity\/Q795065","author":{"@type":"Organization","name":"Contributors to Wikimedia projects"},"publisher":{"@type":"Organization","name":"Wikimedia Foundation, Inc.","logo":{"@type":"ImageObject","url":"https:\/\/www.wikimedia.org\/static\/images\/wmf-hor-googpub.png"}},"datePublished":"2003-07-23T12:20:12Z","dateModified":"2024-06-13T05:46:08Z"}</script> </body> </html>