Difference between revisions of "Python: Lambda and List Comprehensions"
From MyWiki
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<source lang="python"> | <source lang="python"> | ||
my_function = lambda a, b, c : a + b | my_function = lambda a, b, c : a + b | ||
+ | my_function(1, 2, 3) | ||
+ | |||
+ | # We will replace the following with a List Comprehension | ||
+ | my_list = [] | ||
+ | for number in range(1,1000): | ||
+ | if number %2 == 0: | ||
+ | my_list.append(number) | ||
+ | |||
+ | my_list | ||
+ | |||
+ | ## Now for the List Comprehension | ||
+ | my_list = [ number for number in range(0,1000) if number % 200 == 0 ] | ||
</source> | </source> |
Latest revision as of 11:36, 28 July 2019
my_function = lambda a, b, c : a + b my_function(1, 2, 3) # We will replace the following with a List Comprehension my_list = [] for number in range(1,1000): if number %2 == 0: my_list.append(number) my_list ## Now for the List Comprehension my_list = [ number for number in range(0,1000) if number % 200 == 0 ]