Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

list comprehension python

# This is the  syntax of a general list comprehension expression in Python
[expression 
 for x1 in it1 if cond1
 for x2 in it2 if cond2
 ...
 for xn in itn if condn
]

# hereby it1,..., itn are iterables and cond1, ..., condn boolean conditions
# expression can depend on x1,...,xn and
# itj and condj can depend on the variables x1,...,xj-1 quantified before it

# The list comprehension expression is equivalent to the following nested 
# for-loop statement
res = []
for x1 in it1:
	if cond1:
  		for x2 in it2: 
      		if cond2:
    			...
      			for xn in itn if condn:
                	if condn:
       					res.append(expression)
Source by www.8bitavenue.com #
 
PREVIOUS NEXT
Tagged: #list #comprehension #python
ADD COMMENT
Topic
Name
6+4 =