0
list comprehensions
i'm finding it hard to understand this list
3 Answers
+ 8
name = [expression for item in iterable if condition]
is equivalent to
name = []
for item in iterable:
if condition:
name.append(expression)
+ 1
A list comprehension is just a simplified and convenient way of generating a list, which could be written also with a for loop and if condition (in 4-5 lines)
0
I know list comprehension from Haskell (programming language). Ther it means to generate a list in passed steps (and optionally a condition), for example all elements from 0 to 20 in a step size of 2, which are lesser 10 would return [0, 2, 4, 6, 8]. How to implement it depends on the programming language...