Using filter() function
, you can take a list and apply a filtering statement (such as x < 100).
In this case, you will end up with a filtered list of value below 100. This concept can be elaborated and used in many different ways.
In plain English here is the difference between map and filter functions:
If you have a function such as:
- multiply by 100 (x*100)
- divide by 10 (x/10)
- convert to integers (int(x))
- convert to lower characters (lower(x))
then map function is suitable for use.
But if you have conditions such as:
- Filter below 100 ( x<100 )
- Filter words only if first letter is capital ( upper(x[0]) == True )
- Filter if length is over 10 ( len(x) > 10 )
- Filter if value is positive ( x > 0 )
then filter function can be used to filter based on these conditions. If the difference that map applies a function to sequence and filter filters a sequence based on condition is not clear, you might want to reflect on that part a bit before moving on.