Hello, today I will be explaining the use of different list functions and how to use lambda. The functions we will be looking at are:
- lambda
- map()
- filter()
- reduce()
[color=#0000CD (Click to View)
lambda[/color]]
Before we get into the list functions, lets look at lambda. Lambda is basically a shortened version of "def", it only takes one line and is used mainly for shorter code. Lets look at using lambda in the interactive prompt.
As you can see we defined a function that takes one argument, and returns the argument divided by two. Lambda can take any number of arguments but if you want a large function that will be used over and over it is safer to use def.
Before we get into the list functions, lets look at lambda. Lambda is basically a shortened version of "def", it only takes one line and is used mainly for shorter code. Lets look at using lambda in the interactive prompt.
Code:
>>> a = lambda x: x / 2
>>> a(4)
2
>>> a(16)
8
>>>
[color=#FF0000 (Click to View)
map()[/color]]
Now lets try using lambda along with the map() function. map() takes two arguments, the first is a function, the second is a list. It will "evaluate" the function on each item in the list a spit out a new list, lets see:
I made a list, and I gave map the list and told it to return each number divided by two and plus five. It basically takes each number in the list "z" as the argument "x" in our lambda function, and returns a new list.
Now lets try using lambda along with the map() function. map() takes two arguments, the first is a function, the second is a list. It will "evaluate" the function on each item in the list a spit out a new list, lets see:
Code:
>>> z = [2, 6, 8, 10, 14, 18, 20]
>>> print map(lambda x: x / 2 + 5, z)
[6, 8, 9, 10, 12, 14, 15]
>>>
[color=#32CD32 (Click to View)
filter()[/color]]
Now we will use the lambda with the filter() function. It has the same syntax as map(), but works a little bit differently. Its syntax is : filter(function, list) but instead of evaluating the function on each item and returning a new list, it returns a shortened list in which each item fits the criteria provided. It is somewhat confusing but it basically works like this:
As you can see we did x % 2, which divides x by two until it reaches a non-whole number or 0. The new list is a list of multiples of 2. Basically filter() creates a list of all the items in the given list that return true(8, 14, 18, 24). Perhaps this will make things more clear.
Now we will use the lambda with the filter() function. It has the same syntax as map(), but works a little bit differently. Its syntax is : filter(function, list) but instead of evaluating the function on each item and returning a new list, it returns a shortened list in which each item fits the criteria provided. It is somewhat confusing but it basically works like this:
Code:
>>> z = [1, 3, 8, 9, 13, 14, 18, 24]
>>> print filter(lambda x : x % 2 == 0, z)
[8, 14, 18, 24]
>>>
Code:
>>> 8 % 2 ==0
True
>>> 14 % 2 ==0
True
>>> 18 % 2 ==0
True
>>> 24 % 2 ==0
True
>>> 1 % 2==0
False
>>>
[color=#00BFFF (Click to View)
reduce()[/color]]
reduce() takes two arguments just like map() and filter(), its syntax is reduce(function, list) The difference here is that the function given needs to have two variables. Basically it takes the first two items on the list and treats them as the first two variables, then it takes the results of that and treats it as the first variable, and repeats for each item on the list. This one is also a little confusing, so lets see it in action:
As you can see our lambda function has two arguments. Here is how it works, it takes the first item of the list (40) and the subtracts the second (20), then it takes the result of that, (20) and subtracts the next item (10). This is basically what it does:
40-20 = 20
20 - 10 = 10
10 - 5 = 5
5 - 3 = 2
We can do other things such as multiply and add as well.
Thank you for reading my somewhat short tutorial and I hope it helped. If you have any questions feel free to ask
reduce() takes two arguments just like map() and filter(), its syntax is reduce(function, list) The difference here is that the function given needs to have two variables. Basically it takes the first two items on the list and treats them as the first two variables, then it takes the results of that and treats it as the first variable, and repeats for each item on the list. This one is also a little confusing, so lets see it in action:
Code:
>>> z = [40, 20, 10, 5, 3]
>>> print reduce(lambda x, y: x - y, z)
2
>>>
40-20 = 20
20 - 10 = 10
10 - 5 = 5
5 - 3 = 2
We can do other things such as multiply and add as well.
Code:
>>> print reduce(lambda x, y: x + y, z)
78
>>> print reduce(lambda x, y: x * y, z)
120000
>>>