Python supports a concept called "list comprehensions". It can be used to construct lists in a very natural, easy way, like a mathematician is used to do.
Advanced List Processing - List Comprehension AU : Dec.-19, Marks 6
•
Python supports a concept called "list comprehensions". It can be
used to construct lists in a very natural, easy way, like a mathematician is
used to do.
•
In Python, we can write the expression almost exactly like a mathematics with
special cryptic syntax.
•
For example:
>>>
L = (x**2 for x in range(10)]
>>>L
[0,
1, 4, 9, 16, 25, 36, 49, 64, 81]
>>>
•
The above code creates a list of square numbers from 0 to 10. This list can be
created using a simplified expression “x**2 for x in range(10)". This is
basically list comprehension.
•
List comprehensions apply an arbitrary expression to items in an iteration.
•
The list comprehension is achieved using the tools like map, lambda and filter.
1.
Map
•
The map() function applies the values to every member of the list and returns
the result.
•
The map function takes two arguments -
result
= map(function, sequence)
For
example
>>>
def increment_by_three(x):
return
x+3
>>>
new_list=list(map(increment_by_three,range(10)))
>>>
new_list
[3,
4, 5, 6, 7, 8, 9, 10, 11, 12]
>>>
Another
Example :
There
is a built in function for calculating length of the string and i.e. len( ). We
can pass this function to map and find the length of every element present in
the list as a list.
>>>
names=['Sandip', 'Lucky','Mahesh','Sachin']
>>>
lengths=list(map(len, names))
>>>
lengths
[6,
5, 6, 6]
>>>
2.
Filter
•
The Filter( ) function selects some of the elements and filters out others. If
we use
•
if with list compression, it is almost equivalent to the filter built-in.
•
This function takes two arguments -
result
= filter(function, sequence)
•
For example –
>>>
def even_fun(x):
return
x%2= = 0
>>>
r = filter(even_fun,(1,2,3,4,5))
>>>
list(r) [2, 4]
>>>
•
In above example, using filter( ) function the odd numbers are filtered
out and only even numbers are displayed.
•
filter( ) works in the similar manner like map( ), it applies function to all
the elements of the list.
3.
Lambda
•
The lambda function is a simple inline function that is used to evaluate the expression
using the given list of arguments.
•
The syntax is
lambda
argument_list
: expression
where
the argument_list consists of a comma separated list of arguments and
the expression is an arithmetic expression using these arguments.
•
For example:
>>>
sum = lambda x,y:x+y
>>>
sum(1,3)
4
>>>
Thus
the expression x+y is evaluated and the result of evaluation is returned.
4.
Reduce
•
The kind of function that combines sequence of elements into a single value is
called reduce function.
•
The reduce function also takes two arguments -
Result
= reduce(function, sequence)
•
The function reduce() had been dropped from the core of Python when
migrating to Python 3. Hence for using the reduce function we have to import
functools to be capable of using reduce.
•
For example
>>>
import functools
>>>
functools.reduce(lambda x,y:x+y,[1,2,3,4])
10
>>>
Example
4.4.1 Following is a list of items purchased
Write
a python program, which returns a list with 2 tuples - Each tuple consists of
item no, and product of price and quantity.
If
value of the order is less than 100 then the product is increased by 10. Write
a program in Python using map and lambda functions
Solution
:
>>>
order=[["111", 12,20],["112",5,100],["113",7,5]]
>>>
min_order=100
>>>
Total_Bill=list(map(lambda x:x if x[1]>=min_order else(x[0],x[1]+10),
map(lambda
x:(x[0],x[1]*x[2]),order)))
>>>
print(Total_Bill)
[('111',
240), ('112', 500), ('113', 45)]
>>>
Example
4.4.2 Write a Python program to display power of 2
using Anonymous function.
Solution
:
The
Anonymous function is a lambda function. We can write the program to display
the power of 2 using Anonymous function
Test.py
Review Question
1. What is list comprehension in Python ? Explain with example.
AU : Dec.-19, Marks 6
Problem Solving and Python Programming: UNIT IV: Lists, Tuples, Dictionaries : Tag: Engineering Python : Python Programming - Advanced List Processing - List Comprehension
Problem Solving and Python Programming
GE3151 1st Semester | 2021 Regulation | 1st Semester Common to all Dept 2021 Regulation