Comparison Of Programming Languages (list Comprehension)
   HOME

TheInfoList



OR:

List comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
is a
syntactic In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituency), ...
construct available in some
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s for creating a list based on existing lists. It follows the form of the mathematical ''
set-builder notation In set theory and its applications to logic, mathematics, and computer science, set-builder notation is a mathematical notation for describing a set by enumerating its elements, or stating the properties that its members must satisfy. Defining ...
'' (''set comprehension'') as distinct from the use of
map A map is a symbolic depiction emphasizing relationships between elements of some space, such as objects, regions, or themes. Many maps are static, fixed to paper or some other durable medium, while others are dynamic or interactive. Although ...
and
filter Filter, filtering or filters may refer to: Science and technology Computing * Filter (higher-order function), in functional programming * Filter (software), a computer program to process a data stream * Filter (video), a software component tha ...
functions.


Examples of list comprehension


Boo

List with all the doubles from 0 to 10 (exclusive) doubles = *2 for i in range(10) List with the names of the customers based in
Rio de Janeiro Rio de Janeiro ( , , ; literally 'River of January'), or simply Rio, is the capital of the state of the same name, Brazil's third-most populous state, and the second-most populous city in Brazil, after São Paulo. Listed by the GaWC as a b ...
rjCustomers = ustomer.Name for customer in customers if customer.State

"RJ"


C#

var ns = from x in Enumerable.Range(0, 100) where x * x > 3 select x * 2; The previous code is
syntactic sugar In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
for the following code written using lambda expressions: var ns = Enumerable.Range(0, 100) .Where(x => x * x > 3) .Select(x => x * 2);


Ceylon

Filtering numbers divisible by 3: value divisibleBy3 = ; // type of divisibleBy3 is Iterable Multiple "generators": value triples = ; // type of triples is Iterable


Clojure

An infinite lazy sequence: (for (iterate inc 0) :when (> (* x x) 3) (* 2 x)) A list comprehension using multiple generators: (for (range 20) y (range 20) z (range 20) :when (

(+ (* x x) (* y y)) (* z z))
y z


CoffeeScript

largeNumbers = (number for number in list when number > 100)


Common Lisp

List comprehensions can be expressed with the loop macro's collect keyword. Conditionals are expressed with if, as follows: (loop for x from 0 to 100 if (> (* x x) 3) collect (* 2 x))


Cobra

List the names of customers: names = for cust in customers get cust.name List the customers with balances: names = for cust in customers where cust.balance > 0 List the names of customers with balances: names = for cust in customers where cust.balance > 0 get cust.name The general forms: for VAR in ENUMERABLE here CONDITIONget EXPR for VAR in ENUMERABLE where CONDITION Note that by putting the condition and expression ''after'' the variable name and enumerable object, editors and IDEs can provide
autocompletion Autocomplete, or word completion, is a feature in which an application predicts the rest of a word a user is typing. In Android and iOS smartphones, this is called predictive text. In graphical user interfaces, users can typically press the tab ...
on the members of the variable.


Dart

or (var i in range(0, 100)) if (i * i > 3) i * 2 var pyth = for (var x in range(1, 20)) for (var y in range(x, 20)) for (var z in range(y, 20)) if (x * x + y * y

z * z) [x, y, z
];
Iterable range(int start, int end) => List.generate(end - start, (i) => start + i);


Elixir

for x <- 0..100, x * x > 3, do: x * 2


Erlang

L = lists:seq(0,100). S = , X <- L, X*X > 3


F#

Lazily-evaluated sequences: seq Or, for floating point values seq Lists and arrays: for x in 0. .. 100. do if x**2. > 3. then yield 2.*x (0..100).findAll.collect_


__Haskell_

_x_<-_ _*_2_.html" ;"title="_.._99">_*_2_">_x_<-_[0_.._99_x_*_x_>_3 An_example_of_a_list_comprehension_using_multiple_generators: pyth_=_ _x_<-_ ..20_y_<-_[x..20_z_<-_[y..20_x^2_+_y^2_

_z^2.html" ;"title="..20_z_<-_[y..20.html" ;"title="..20_y_<-_[x..20_z_<-_[y..20">..20_y_<-_[x..20_z_<-_[y..20_x^2_+_y^2_

_z^2">..20_z_<-_[y..20.html" ;"title="..20_y_<-_[x..20_z_<-_[y..20">..20_y_<-_[x..20_z_<-_[y..20_x^2_+_y^2_

_z^2


__Io_

By_using_Range_object,_Io_language_can_create_list_as_easy_as_in_other_languages: Range_0_to(100)_asList_select(x,_x*x>3)_map(*2)_


__ISLISP_

List_comprehensions_can_be_expressed_with_the_for_special_form._Conditionals_are_expressed_with_if,_as_follows: (for_((x_0_(+_x_1)) ______(collect_())) _____((>=_x_100)_(reverse_collect)) _____(if_(>_(*_x_x)_3) _________(setq_collect_(cons_(*_x_2)_collect))))


__Java_

Java_with_the_Streams_API,_which_includes_the_IntStream_interface_which_allows_operations_like_the_following: List_ns_=_IntStream.range(0,_100) ________.filter(x_->_x_*_x_>_3) ________.map(x_->_x_*_2) ________.boxed().collect(Collectors.toList());


__JavaScript_

[...new_Array(100).keys()].filter(x_=>_x**2_>_3).map(x_=>_2_*_x);


__Julia_

Julia_supports_comprehensions_using_the_syntax: _y_=_[x^2+1_for_x_in_1:10] and_multidimensional_comprehensions_like: _z_=_ x-5)^2+(y-5)^2_for_x_=_0:10,_y_=_0:10 It_is_also_possible_to_add_a_condition: v_=_ x^2_+_2y^2_for_x_in_1:7_for_y_in_1:7_if_x_%_y_

_0
And_just_changing_square_brackets_to_the_round_one,_we_get_a_generator: g_=_(3x^2_+_2y^2_for_x_in_1:7_for_y_in_1:7_if_x_%_y_

_0)


__Mythryl_

__s_=_ 2*i_for_i_in_1..100_where_i*i_>_3_ Multiple_generators: __pyth_=_ (x,y,z)_for_x_in_1..20_for_y_in_x..20_for_z_in_y..20_where_x*x_+_y*y_

_z*z_


__Nemerle_

$ _x_in_ _.._100_x*x_>_3


__Nim_

Nim_has_built-in_seq,_set,_table_and_object_comprehensions_on_the_sugar_standard_library_module: import_sugar let_variable_=_collect(newSeq): __for_item_in_@ 9,_1,_42,_0,_-1,_9_item_+_1 assert_variable_

_@ 8,_2,_43,_1,_0,_10 The_comprehension_is_implemented_as_a_macro_that_is_expanded_at_compile_time,_ you_can_see_the_expanded_code_using_the_expandMacro_compiler_option: var_collectResult_=_newSeq(Natural(0)) for_item_in_items(@ 9,_1,_42,_0,_-1,_9: __add(collectResult,_item_+_1) collectResult The_comprehensions_can_be_nested_and_multi-line: import_sugar let_values_=_collect(newSeq): __for_val_in_
,_2 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
____collect(newSeq): ______for_val2_in_
,_4 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
________if_(val,_val2)_!=_(1,_2): __________(val,_val2) ________ assert_values_

_@
[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)">1,_3),_(1,_4).html"_;"title="[(1,_3),_(1,_4)">[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)


__OCaml_

''OCaml_supports_List_comprehension_through_OCaml_Batteries''.


__Perl_

Array_with_all_the_doubles_from_1_to_9_inclusive: my_@doubles_=_map__1..9; Array_with_the_names_of_the_customers_based_in_Rio_de_Janeiro_(from_array_of_hashes): my_@rjCustomers_=_map__@customers; Filtering_numbers_divisible_by_3: my_@divisibleBy3_=_grep__0..100;


__Python_

Python_(programming_language).html" "title="1,_3),_(1,_4)_@[(2,_3),_(2,_4).html" ;"title="1,_3),_(1,_4).html" ;"title="[(1,_3),_(1,_4)">[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)">1,_3),_(1,_4).html" ;"title="[(1,_3),_(1,_4)">[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)


__OCaml_

''OCaml_supports_List_comprehension_through_OCaml_Batteries''.


__Perl_

Array_with_all_the_doubles_from_1_to_9_inclusive: my_@doubles_=_map__1..9; Array_with_the_names_of_the_customers_based_in_Rio_de_Janeiro_(from_array_of_hashes): my_@rjCustomers_=_map__@customers; Filtering_numbers_divisible_by_3: my_@divisibleBy3_=_grep__0..100;


__Python_

Python_(programming_language)">Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
_uses_the_following_syntax_to_express_list_comprehensions_over_finite_lists: S_=_[2_*_x_for_x_in_range(100)_if_x_**_2_>_3] A_generator_expression_may_be_used_in_Python_versions_>=_2.4_which_gives_lazy_evaluation_over_its_input,_and_can_be_used_with_generator_(computer_science), generators_to_iterate_over_'infinite'_input_such_as_the_count_generator_function_which_returns_successive_integers: from_itertools_import_count S_=_(2_*_x_for_x_in_count()_if_x_**_2_>_3) (Subsequent_use_of_the_generator_expression_will_determine_when_to_stop_generating_values).


__R_

_x_<-_0:100 _S_<-_2_*_x _^_2_>_3


__Racket_

(for/list_( _100#:when_(>_(*_x_x)_3))_(*_x_2)) An_example_with_multiple_generators: (for*/list_( _(in-range_1_21) _(in-range_1_21) _(in-range_1_21)____________#:when_(=_(+_(*_x_x)_(*_y_y))_(*_z_z))) __(list_x_y_z))


__Raku_

_my_@s_=_($__*_2_if_$__**_2_>_3_for_0_.._99);


__Ruby_

(0..100).select_.map_


__Rust_

Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
_does_not_have_built-in_list_comprehensions,_but_many_of_their_capabilities_can_be_replicated_with_iterators: let_ns:_Vec<_>_=_(0..100).filter(, x, _x_*_x_>_3).map(, x, _2_*_x).collect();


__Scala_

Using_the_for-comprehension: val_s_=_for_(x_<-_0_to_100;_if_x*x_>_3)_yield_2*x


__Scheme_

List_comprehensions_are_supported_in_Scheme_through_the_use_of_the_ SRFI-42_library.Scheme_SRFI_42:_Eager_Comprehensions
/ref> (list-ec_(:_x_100)_(if_(>_(*_x_x)_3))_(*_x_2)) An_example_of_a_list_comprehension_using_multiple_generators: (list-ec_(:_x_1_21)_(:_y_x_21)_(:_z_y_21)_(if_(=_(+_(*_x_x)_(*_y_y))_(*_z_z)))_(list_x_y_z))


__SETL_

s_:=_;


__Smalltalk_

((1_to:_100)_select:_ _x_squared_>_3__collect:_ _x_*_2_


__Swift_

//_0_2_4_6_..._18 let_timesTwo_=_(0..<10).map //_Suppose_isPrime:_(Int)_->_Bool_a_function_that_checks_if_its_argument_is_a_prime_number let_primeBelow100_=_(0...100).filter(isPrime)


__Visual_Prolog_

_S_=_[_2*X_, , _X_=_list::getMember_nd(L),_X*X_>_3_]


__PowerShell_

$s_=_(_0..100_, _?__, _%__) which_is_short-hand_notation_of: $s_=_0..100_, _where-object__, _foreach-object_


__References_

{{Reflist


__External_links_


Comparison_of_list_comprehensions_on_rosettacode.org
*List_comprehension Articles_with_example_C_Sharp_code Articles_with_example_Haskell_code Articles_with_example_Lisp_(programming_language)_code Articles_with_example_Python_(programming_language)_code Articles_with_example_Racket_code Articles_with_example_Julia_code fr:Compréhension_de_liste pt:List_comprehensionhtml" ;"title=" for x in 0. .. 100. do if x**2. > 3. then yield 2.*x , ] List comprehensions are the part of a greater family of language constructs called computation expressions.


Groovy

(0..100).findAll.collect


Haskell

x <- _*_2_.html" ;"title=" .. 99"> * 2 "> x <- [0 .. 99 x * x > 3 An example of a list comprehension using multiple generators: pyth = x <- ..20_y_<-_[x..20_z_<-_[y..20_x^2_+_y^2_

_z^2.html" ;"title="..20_z_<-_[y..20.html" ;"title="..20 y <- [x..20 z <- [y..20">..20 y <- [x..20 z <- [y..20 x^2 + y^2

z^2">..20_z_<-_[y..20.html" ;"title="..20 y <- [x..20 z <- [y..20">..20 y <- [x..20 z <- [y..20 x^2 + y^2

z^2


Io

By using Range object, Io language can create list as easy as in other languages: Range 0 to(100) asList select(x, x*x>3) map(*2)


ISLISP

List comprehensions can be expressed with the for special form. Conditionals are expressed with if, as follows: (for ((x 0 (+ x 1)) (collect ())) ((>= x 100) (reverse collect)) (if (> (* x x) 3) (setq collect (cons (* x 2) collect))))


Java

Java with the Streams API, which includes the IntStream interface which allows operations like the following: List ns = IntStream.range(0, 100) .filter(x -> x * x > 3) .map(x -> x * 2) .boxed().collect(Collectors.toList());


JavaScript

[...new Array(100).keys()].filter(x => x**2 > 3).map(x => 2 * x);


Julia

Julia supports comprehensions using the syntax: y = [x^2+1 for x in 1:10] and multidimensional comprehensions like: z = x-5)^2+(y-5)^2 for x = 0:10, y = 0:10 It is also possible to add a condition: v = x^2 + 2y^2 for x in 1:7 for y in 1:7 if x % y

0
And just changing square brackets to the round one, we get a generator: g = (3x^2 + 2y^2 for x in 1:7 for y in 1:7 if x % y

0)


Mythryl

s = 2*i for i in 1..100 where i*i > 3 Multiple generators: pyth = (x,y,z) for x in 1..20 for y in x..20 for z in y..20 where x*x + y*y

z*z


Nemerle

$ x in .. 100 x*x > 3


Nim

Nim has built-in seq, set, table and object comprehensions on the sugar standard library module: import sugar let variable = collect(newSeq): for item in @ 9, 1, 42, 0, -1, 9 item + 1 assert variable

@ 8, 2, 43, 1, 0, 10
The comprehension is implemented as a macro that is expanded at compile time, you can see the expanded code using the expandMacro compiler option: var collectResult = newSeq(Natural(0)) for item in items(@ 9, 1, 42, 0, -1, 9: add(collectResult, item + 1) collectResult The comprehensions can be nested and multi-line: import sugar let values = collect(newSeq): for val in
, 2 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
collect(newSeq): for val2 in
, 4 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
if (val, val2) != (1, 2): (val, val2) assert values

@
[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)">1,_3),_(1,_4).html"_;"title="[(1,_3),_(1,_4)">[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)


__OCaml_

''OCaml_supports_List_comprehension_through_OCaml_Batteries''.


__Perl_

Array_with_all_the_doubles_from_1_to_9_inclusive: my_@doubles_=_map__1..9; Array_with_the_names_of_the_customers_based_in_Rio_de_Janeiro_(from_array_of_hashes): my_@rjCustomers_=_map__@customers; Filtering_numbers_divisible_by_3: my_@divisibleBy3_=_grep__0..100;


__Python_

Python_(programming_language).html" "title="1,_3),_(1,_4)_@[(2,_3),_(2,_4).html" ;"title="1,_3),_(1,_4).html" ;"title="[(1, 3), (1, 4)">[(1, 3), (1, 4) @[(2, 3), (2, 4)">1,_3),_(1,_4).html" ;"title="[(1, 3), (1, 4)">[(1, 3), (1, 4) @[(2, 3), (2, 4)


OCaml

''OCaml supports List comprehension through OCaml Batteries''.


Perl

Array with all the doubles from 1 to 9 inclusive: my @doubles = map 1..9; Array with the names of the customers based in Rio de Janeiro (from array of hashes): my @rjCustomers = map @customers; Filtering numbers divisible by 3: my @divisibleBy3 = grep 0..100;


Python

Python (programming language)">Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
uses the following syntax to express list comprehensions over finite lists: S = [2 * x for x in range(100) if x ** 2 > 3] A generator expression may be used in Python versions >= 2.4 which gives lazy evaluation over its input, and can be used with generator (computer science), generators to iterate over 'infinite' input such as the count generator function which returns successive integers: from itertools import count S = (2 * x for x in count() if x ** 2 > 3) (Subsequent use of the generator expression will determine when to stop generating values).


R

x <- 0:100 S <- 2 * x ^ 2 > 3


Racket

(for/list ( 100#:when (> (* x x) 3)) (* x 2)) An example with multiple generators: (for*/list ( (in-range 1 21) (in-range 1 21) (in-range 1 21) #:when (= (+ (* x x) (* y y)) (* z z))) (list x y z))


Raku

my @s = ($_ * 2 if $_ ** 2 > 3 for 0 .. 99);


Ruby

(0..100).select .map


Rust

Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
does not have built-in list comprehensions, but many of their capabilities can be replicated with iterators: let ns: Vec<_> = (0..100).filter(, x, x * x > 3).map(, x, 2 * x).collect();


Scala

Using the for-comprehension: val s = for (x <- 0 to 100; if x*x > 3) yield 2*x


Scheme

List comprehensions are supported in Scheme through the use of the SRFI-42 library.Scheme SRFI 42: Eager Comprehensions
/ref> (list-ec (: x 100) (if (> (* x x) 3)) (* x 2)) An example of a list comprehension using multiple generators: (list-ec (: x 1 21) (: y x 21) (: z y 21) (if (= (+ (* x x) (* y y)) (* z z))) (list x y z))


SETL

s := ;


Smalltalk

((1 to: 100) select: x squared > 3 collect: x * 2


Swift

// 0 2 4 6 ... 18 let timesTwo = (0..<10).map // Suppose isPrime: (Int) -> Bool a function that checks if its argument is a prime number let primeBelow100 = (0...100).filter(isPrime)


Visual Prolog

S = [ 2*X , , X = list::getMember_nd(L), X*X > 3 ]


PowerShell

$s = ( 0..100 , ? , % ) which is short-hand notation of: $s = 0..100 , where-object , foreach-object


References

{{Reflist


External links


Comparison of list comprehensions on rosettacode.org
*List comprehension Articles with example C Sharp code Articles with example Haskell code Articles with example Lisp (programming language) code Articles with example Python (programming language) code Articles with example Racket code Articles with example Julia code fr:Compréhension de liste pt:List comprehension> for x in 0. .. 100. do if x**2. > 3. then yield 2.*x ,
List comprehensions are the part of a greater family of language constructs called computation expressions.


Groovy

(0..100).findAll.collect


Haskell

x <- _*_2_.html" ;"title=" .. 99"> * 2 "> x <- [0 .. 99 x * x > 3 An example of a list comprehension using multiple generators: pyth = x <- ..20_y_<-_[x..20_z_<-_[y..20_x^2_+_y^2_

_z^2.html" ;"title="..20_z_<-_[y..20.html" ;"title="..20 y <- [x..20 z <- [y..20">..20 y <- [x..20 z <- [y..20 x^2 + y^2

z^2">..20_z_<-_[y..20.html" ;"title="..20 y <- [x..20 z <- [y..20">..20 y <- [x..20 z <- [y..20 x^2 + y^2

z^2


Io

By using Range object, Io language can create list as easy as in other languages: Range 0 to(100) asList select(x, x*x>3) map(*2)


ISLISP

List comprehensions can be expressed with the for special form. Conditionals are expressed with if, as follows: (for ((x 0 (+ x 1)) (collect ())) ((>= x 100) (reverse collect)) (if (> (* x x) 3) (setq collect (cons (* x 2) collect))))


Java

Java with the Streams API, which includes the IntStream interface which allows operations like the following: List ns = IntStream.range(0, 100) .filter(x -> x * x > 3) .map(x -> x * 2) .boxed().collect(Collectors.toList());


JavaScript

[...new Array(100).keys()].filter(x => x**2 > 3).map(x => 2 * x);


Julia

Julia supports comprehensions using the syntax: y = [x^2+1 for x in 1:10] and multidimensional comprehensions like: z = x-5)^2+(y-5)^2 for x = 0:10, y = 0:10 It is also possible to add a condition: v = x^2 + 2y^2 for x in 1:7 for y in 1:7 if x % y

0
And just changing square brackets to the round one, we get a generator: g = (3x^2 + 2y^2 for x in 1:7 for y in 1:7 if x % y

0)


Mythryl

s = 2*i for i in 1..100 where i*i > 3 Multiple generators: pyth = (x,y,z) for x in 1..20 for y in x..20 for z in y..20 where x*x + y*y

z*z


Nemerle

$ x in .. 100 x*x > 3


Nim

Nim has built-in seq, set, table and object comprehensions on the sugar standard library module: import sugar let variable = collect(newSeq): for item in @ 9, 1, 42, 0, -1, 9 item + 1 assert variable

@ 8, 2, 43, 1, 0, 10
The comprehension is implemented as a macro that is expanded at compile time, you can see the expanded code using the expandMacro compiler option: var collectResult = newSeq(Natural(0)) for item in items(@ 9, 1, 42, 0, -1, 9: add(collectResult, item + 1) collectResult The comprehensions can be nested and multi-line: import sugar let values = collect(newSeq): for val in
, 2 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
collect(newSeq): for val2 in
, 4 The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
if (val, val2) != (1, 2): (val, val2) assert values

@
[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)">1,_3),_(1,_4).html"_;"title="[(1,_3),_(1,_4)">[(1,_3),_(1,_4)_@[(2,_3),_(2,_4)


__OCaml_

''OCaml_supports_List_comprehension_through_OCaml_Batteries''.


__Perl_

Array_with_all_the_doubles_from_1_to_9_inclusive: my_@doubles_=_map__1..9; Array_with_the_names_of_the_customers_based_in_Rio_de_Janeiro_(from_array_of_hashes): my_@rjCustomers_=_map__@customers; Filtering_numbers_divisible_by_3: my_@divisibleBy3_=_grep__0..100;


__Python_

Python_(programming_language).html" "title="1,_3),_(1,_4)_@[(2,_3),_(2,_4).html" ;"title="1,_3),_(1,_4).html" ;"title="[(1, 3), (1, 4)">[(1, 3), (1, 4) @[(2, 3), (2, 4)">1,_3),_(1,_4).html" ;"title="[(1, 3), (1, 4)">[(1, 3), (1, 4) @[(2, 3), (2, 4)


OCaml

''OCaml supports List comprehension through OCaml Batteries''.


Perl

Array with all the doubles from 1 to 9 inclusive: my @doubles = map 1..9; Array with the names of the customers based in Rio de Janeiro (from array of hashes): my @rjCustomers = map @customers; Filtering numbers divisible by 3: my @divisibleBy3 = grep 0..100;


Python

Python (programming language)">Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
uses the following syntax to express list comprehensions over finite lists: S = [2 * x for x in range(100) if x ** 2 > 3] A generator expression may be used in Python versions >= 2.4 which gives lazy evaluation over its input, and can be used with generator (computer science), generators to iterate over 'infinite' input such as the count generator function which returns successive integers: from itertools import count S = (2 * x for x in count() if x ** 2 > 3) (Subsequent use of the generator expression will determine when to stop generating values).


R

x <- 0:100 S <- 2 * x ^ 2 > 3


Racket

(for/list ( 100#:when (> (* x x) 3)) (* x 2)) An example with multiple generators: (for*/list ( (in-range 1 21) (in-range 1 21) (in-range 1 21) #:when (= (+ (* x x) (* y y)) (* z z))) (list x y z))


Raku

my @s = ($_ * 2 if $_ ** 2 > 3 for 0 .. 99);


Ruby

(0..100).select .map


Rust

Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
does not have built-in list comprehensions, but many of their capabilities can be replicated with iterators: let ns: Vec<_> = (0..100).filter(, x, x * x > 3).map(, x, 2 * x).collect();


Scala

Using the for-comprehension: val s = for (x <- 0 to 100; if x*x > 3) yield 2*x


Scheme

List comprehensions are supported in Scheme through the use of the SRFI-42 library.Scheme SRFI 42: Eager Comprehensions
/ref> (list-ec (: x 100) (if (> (* x x) 3)) (* x 2)) An example of a list comprehension using multiple generators: (list-ec (: x 1 21) (: y x 21) (: z y 21) (if (= (+ (* x x) (* y y)) (* z z))) (list x y z))


SETL

s := ;


Smalltalk

((1 to: 100) select: x squared > 3 collect: x * 2


Swift

// 0 2 4 6 ... 18 let timesTwo = (0..<10).map // Suppose isPrime: (Int) -> Bool a function that checks if its argument is a prime number let primeBelow100 = (0...100).filter(isPrime)


Visual Prolog

S = [ 2*X , , X = list::getMember_nd(L), X*X > 3 ]


PowerShell

$s = ( 0..100 , ? , % ) which is short-hand notation of: $s = 0..100 , where-object , foreach-object


References

{{Reflist


External links


Comparison of list comprehensions on rosettacode.org
*List comprehension Articles with example C Sharp code Articles with example Haskell code Articles with example Lisp (programming language) code Articles with example Python (programming language) code Articles with example Racket code Articles with example Julia code fr:Compréhension de liste pt:List comprehension