Python Diaries Chapter 3 Map | Filter |  For-else | List Comprehension

Author: Divyanshu aka StellarCoderPython 2.xThis is third article of the series–> Python Diaries (Python 2.x)Link for second article –>Python Diaries chapter 2In previous article we discussed print methods, input methods and string manipulation in detail.
In this article we will cover for-else loop,filter function,list comprehension,map function and lambda function.
This one will be a little bit tricky but I will try to keep things as simple as possible.For-else LoopHave you ever used flag in your program? If yes, you will be able to relate it with for-else Loop.flag is an ‘indicator’ variable. It tells us about whether a event took place or not.Let me clarify it through an example.Question: print whether zero is present in the array ‘arr’.If 0 is present, print “Zero is present in the array”, otherwise print “Zero is not present in the array”
for item in arr:
if item==0:

if flag==1:
print “Zero is present in the array”
print “Zero is not present in the array”We could have done this question without the flag also, but I wanted to show you the usage of the flag.Now let us see how to approach above problem usingfor-elseloop.
The’for-else’loop works in the same way as the ‘for’ loop but it has an extra ‘else’ part which gets executed if no ‘break’
is given in the ‘for’ part the’for-else’loop.program 1arr=[10,20,30,40,50,0,60,70,80,90]
for item in arr:
if item==0:
print “Zero is present”
print “Zero is not present in the array”Output -> Zero is present in the arrayNote–> If we had not used thebreakstatement then theelseblock would have also got executed and the output would have beenOutput->
Zero is present in the array
Zero is not present in the arrayBefore moving on to the next topics, let me first give you a brief idea about syntax of function.I will explain it thoroughly in upcoming articles but you need to have a basic idea of syntax of a function to understand following topics.Functionssyntax–>def name_of_function(arguments_separated_by_comma):
your code/logic
return whatever_you_want_to
# returning something is not compulsoryExapmledef sum_of_numbers(a,b):
return c

print sum_of_numbers(2,3)Output–>5List comprehensionList comprehension is a way to define and create list.
suppose, for numbers 1 to 10, you want to create a list which has squares of these numbers ie.
[1,4,9,16,25,36,49,64,81,100]Now to create that list we will use list comprehensionsyntax–>array_name = [expression(i) for i in old_array_name if filter(i)]or[ expression for item in list if conditional ]expression is the operation you want to perform on ‘i’, it can be function or any simple expression.
then we loop through items present in the given list using which we will make new list or we can use range() to make the list.Ifconditional is optional to filter the elements.Let us see some examplesprogram 1list_of_squares=[x**2 for x in range(1,11)]Output–> [1,4,9,16,25,36,49,64,81,100]program 2# List of squares of odd numbers
list_of_squares=[x**2 for x in range(1,11) if x%2==1]Output–>[1, 9, 25, 49, 81]program 3def quadratic(x):
y=x**2 + 5*x + 9
return y

list_of_quadratic=[quadratic(x) for x in range(4,9)]Output–>[45, 59, 75, 93, 113]and so on…..We can also use list comprehension to create multi-dimesional arrays, check the following link for the same applies the function to every item of iterable(list,tuple etc..) and returns a list of the results.Syntax of map function ->map(function, iterable[,optional args])Function can be a normal function or a lambda function.Lambda function is a function which doesn’t have name ie.Anonymous Function.Example of Lambda functionmagic=lambda x:x**2 + 5*x + 9
magic(4)Output–> 45Syntax of lambda functionfirst of all write ‘lambda’ then a dummy variable(‘x’ in our case) and then the expression separated by colon.Now let us look at some examples of map functionProgram 1arr=range(1,10)
list_of_quadratics=map(lambda x:x**2+ 5*x + 9,arr)
print list_of_quadratics
#Default return data type of map is list, so no need of using square brackets ‘[ ]’ or ‘list()’Output–> [15, 23, 33, 45, 59, 75, 93, 113, 135]Note–> If we had used’list()’ then it would have made no difference and if we had used'[ ]’then we would have got a 2-d array with this array at 0th index.Program 2#Converts string to uppercase
def To_Upper_Case(x):
return x

print our_arrayOutput–>[‘HARVEY’, ‘SPECTER’, ‘IS’, ‘AWESOME’]FilterIt returns a list of those elements for which the function used/passed returns True and filters out
the value which return False or None.Syntaxfilter(function,iterable)Again function can be lambda also.Examples of filter functionProgram 1arr=range(1,60)
final_list=filter(lambda x:x>50,arr)
print final_listOutput–> [51, 52, 53, 54, 55, 56, 57, 58, 59]Program 2arr=range(1,20)
final_array=filter(lambda x:x%2==1,arr)
print final_arrayOutput-> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]Similarly, you can customize function and list according to your requirement and use combination of filter function,lambda functions,
map function and list comprehension.

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *