#Python function def func_name(param): return param + 10 #Python lambda func_name = lambda param: param + 10
Looking at the following example, "nums" is original copy, and "doubles" is new copy. We don’t corrupt "nums" after running map function.
Map vs. Map
nums = [ 1, 2, 3, 4, 5 ] def double(x): return x * 2 doubles = map(double, nums)
You can use Python lambda here to simplify it. Just one line of code…. Awesome!
doubles = list(map(lambda x: x * 2, nums)) #Result: [ 2, 4, 6, 8, 10 ]
const nums = [1, 2, 3, 4, 5] const doubles = nums.map( x => x * 2); console.log(doubles) //Result: [2, 4, 6, 8, 10]
Let's try to take even numbers out from given list.
nums = [1,2,3,4,5,6] evens = list(filter(lambda x : x % 2 == 0, nums)) #Result: [2, 4, 6]
const nums = [1,2,3,4,5,6] const evens = nums.filter( x => x % 2 == 0 ) console.log(evens) //Result: [2,4,6]