0

list comprehensions

i'm finding it hard to understand this list

17th Jan 2019, 6:11 PM
umoru divine
umoru divine - avatar
3 Answers
+ 8
name = [expression for item in iterable if condition] is equivalent to name = [] for item in iterable: if condition: name.append(expression)
17th Jan 2019, 7:06 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 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)
17th Jan 2019, 6:56 PM
Tibor Santa
Tibor Santa - avatar
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...
17th Jan 2019, 6:23 PM
Niwo
Niwo - avatar