Python - Comprehension (Iteration)

Card Puncher Data Processing

About

Comprehension are a way to loop over elements of:

Comprehensions permit to build collections out of other collections.

Comprehensions allow one to express computations over the elements of a set, list, or dictionary without a traditional for-loop.

Use of comprehensions leads to more compact and more readable code, code that more clearly expresses the mathematical idea behind the computation being expressed.

Comprehensions are implemented using a function scope.

Syntax

IF as a filter

An IF that filter a sequence

[ EXP for x in seq if COND ]

Example

>>> list = [1, 2, 3]
>>> [x for x in list if x == 2]
[2]

IF as a ternary

An IF as a ternary operator to transform the element of the sequence

[ EXP IF condition then EXP else EXP for x in seq ]

Example

>>> list = [1, 2, 3]
>>> [x if x==2 else 0 for x in list]
[0, 2, 0]

Example

Initialization

S = [2 * x for x in range(101) if x ** 2 > 3]

Count Number of Occurrence in a List

The following piece of code create a dictionary with as key the element and as value, the number of occurrence of the element in the list

>>> tokens
[1, 2, 3, 2]
>>> {x:tokens.count(x) for x in tokens}
{1: 1, 2: 2, 3: 1}

Documentation / Reference





Discover More
Card Puncher Data Processing
Language - Loop (For, While) - (Break, Continue)

Repeating a set of actions until a certain condition (predicate) fails is the job of programming loops; loops can take different forms, but they all satisfy this basic behavior. A loop includes: ...
Card Puncher Data Processing
Python - (Dictionary|Map)

A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. A dictionary is suitable for representing functions with finite domains....
Card Puncher Data Processing
Python - (Loop|Iteration)

Python - (Loop|Iteration) * * * See: Iterator Types * ...
Card Puncher Data Processing
Python - Combination Codebook

This code can use only numbers. An other processing is needed to apply it to string or symbol. output: With itertools, all characters are allowed. output: Recursive comprehension: Generation...
Card Puncher Data Processing
Python - List

Lists are used to store a collection of different pieces of information in a sequence order of under a single variable name. A list can be used to implement a . A list is mutable whereas a tuple is not....
Card Puncher Data Processing
Python - Set

Implementation of a set data structure in Python. Sets are mutable. There is a non-mutable version of set called frozenset. The elements of a set arenot mutable. A set then cannot contain a list since...
Card Puncher Data Processing
Python - Tuple

Tuples are: an ordered sequence of elements, just like lists. immutable so they can be elements of sets but they can contain mutable objects. A tuple consists of a number of values separated...
Card Puncher Data Processing
Python - Zip (Collection)

A zip is a collection constructed from other collections all of the same length. Zip create pairs of elements when passed two lists, and will stop at the end of the shorter list. Each element of the...



Share this page:
Follow us:
Task Runner