Tutorial by Examples: comprehensions

A list comprehension creates a new list by applying an expression to each element of an iterable. The most basic form is: [ <expression> for <element> in <iterable> ] There's also an optional 'if' condition: [ <expression> for <element> in <iterable> if <c...
A dictionary comprehension is similar to a list comprehension except that it produces a dictionary object instead of a list. A basic example: Python 2.x2.7 {x: x * x for x in (1, 2, 3, 4)} # Out: {1: 1, 2: 4, 3: 9, 4: 16} which is just another way of writing: dict((x, x * x) for x in (1, 2...
Set comprehension is similar to list and dictionary comprehension, but it produces a set, which is an unordered collection of unique elements. Python 2.x2.7 # A set containing every value in range(5): {x for x in range(5)} # Out: {0, 1, 2, 3, 4} # A set of even numbers between 1 and 10: {x f...
Given a list comprehension you can append one or more if conditions to filter values. [<expression> for <element> in <iterable> if <condition>] For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function...
List Comprehensions can use nested for loops. You can code any number of nested for loops within a list comprehension, and each for loop may have an optional associated if test. When doing so, the order of the for constructs is the same order as when writing a series of nested for statements. The ge...
If you have several objects of monadic types, we can achieve combinations of the values using a 'for comprehension': for { x <- Option(1) y <- Option("b") z <- List(3, 4) } { // Now we can use the x, y, z variables println(x, y, z) x // the last expre...
The filter or map functions should often be replaced by list comprehensions. Guido Van Rossum describes this well in an open letter in 2005: filter(P, S) is almost always written clearer as [x for x in S if P(x)], and this has the huge advantage that the most common usages involve predicates tha...
The for clause of a list comprehension can specify more than one variable: [x + y for x, y in [(1, 2), (3, 4), (5, 6)]] # Out: [3, 7, 11] [x + y for x, y in zip([1, 3, 5], [2, 4, 6])] # Out: [3, 7, 11] This is just like regular for loops: for x, y in [(1,2), (3,4), (5,6)]: print(x+y) ...
Elixir doesn't have loops. Instead of them for lists there are great Enum and List modules, but there are also List Comprehensions. List Comprehensions can be useful to: create new lists iex(1)> for value <- [1, 2, 3], do: value + 1 [2, 3, 4] filtering lists, using guard expressi...
Haskell has list comprehensions, which are a lot like set comprehensions in math and similar implementations in imperative languages such as Python and JavaScript. At their most basic, list comprehensions take the following form. [ x | x <- someList ] For example [ x | x <- [1..4] ] -...
for comprehensions in Scala are just syntactic sugar. These comprehensions are implemented using the withFilter, foreach, flatMap and map methods of their subject types. For this reason, only types that have these methods defined can be utilized in a for comprehension. A for comprehension of the fo...
With Parallel List Comprehensions language extension, [(x,y) | x <- xs | y <- ys] is equivalent to zip xs ys Example: [(x,y) | x <- [1,2,3] | y <- [10,20]] -- [(1,10),(2,20)]
Consider the following list comprehension Python 2.x2.7 i = 0 a = [i for i in range(3)] print(i) # Outputs 2 This occurs only in Python 2 due to the fact that the list comprehension “leaks” the loop control variable into the surrounding scope (source). This behavior can lead to hard-to-find...
Nested list comprehensions, unlike list comprehensions with nested loops, are List comprehensions within a list comprehension. The initial expression can be any arbitrary expression, including another list comprehension. #List Comprehension with nested loop [x + y for x in [1, 2, 3] for y in [3, 4...
You can do neat things via the results of Array "comprehensions"... Like assign multiple variables... from the result of a looping for statement... [express,_] = (require x for x in ['express','underscore']) Or a syntactically sweet version of a "mapped" function call, etc.....
Options have a flatMap method. This means they can be used in a for comprehension. In this way we can lift regular functions to work on Options without having to redefine them. val firstOption: Option[Int] = Option(1) val secondOption: Option[Int] = Option(2) val myResult = for { firstValue ...
Python allows you to hack list comprehensions to evaluate conditional expressions. For instance, [value_false, value_true][<conditional-test>] Example: >> n = 16 >> print [10, 20][n <= 15] 10 Here n<=15 returns False (which equates to 0 in Python). So what Python i...
Nested for loops may be used to iterate over a number of unique iterables. result = [] for a = iterable_a for b = iterable_b push!(result, expression) end end Similarly, multiple iteration specifications may be supplied to an array comprehension. [expression for a = iterabl...
Generator comprehensions follow a similar format to array comprehensions, but use parentheses () instead of square brackets []. (expression for element = iterable) Such an expression returns a Generator object. julia> (x^2 for x = 1:5) Base.Generator{UnitRange{Int64},##1#2}(#1,1:5) Fun...

Page 1 of 1