0% found this document useful (0 votes)
64 views2 pages

Lab 8 Python

The document contains several code snippets demonstrating the use of Python's functional programming features such as map, filter, and reduce. It showcases operations like doubling values in a list, sorting based on unique character counts, filtering based on string length, and combining lists. Each code block is followed by its output, illustrating the results of the operations performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views2 pages

Lab 8 Python

The document contains several code snippets demonstrating the use of Python's functional programming features such as map, filter, and reduce. It showcases operations like doubling values in a list, sorting based on unique character counts, filtering based on string length, and combining lists. Each code block is followed by its output, illustrating the results of the operations performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1) l=[2,4,5,6]

l1 = map(lambda x : x*2,l)
print(list (l1))

[4, 8, 10, 12]

2)

l2 = ['abc','ab','a','xyz','pqr']
s = sorted(l2, key=lambda x : len(set(x)))
s1 = sorted(l2, key=lambda x : len(set(x)), reverse=True)
print('Acending order :' ,s)
print('Descending order :',s1)

Acending order : ['a', 'ab', 'abc', 'xyz', 'pqr']


Descending order : ['abc', 'xyz', 'pqr', 'ab', 'a']

3)

l3 = ['abc','xya1','abcdef','wxyz','lmnopq']
filtered_list = list (filter(lambda x : len(x)>5 , l3))
print(filtered_list)

['abcdef', 'lmnopq']

4)

l4=["Mango","#trawberry","apple","#range"]
s=filter(lambda x:x[0]=='#',l4)
print(list(s))

['#trawberry', '#range']

5) from functools import reduce

l = [[1,2,3],[4,5,6],[7,8]]
l1 = lambda x,y : x+y
add = reduce(l1,l)
print(list(add))
[1, 2, 3, 4, 5, 6, 7, 8]

6)

a= [2,-4,4,-3,6,-7,]
l = list(filter(lambda x : x<0, a ))
l1 = list(filter(lambda x : x>=0,a))
print(l+l1)

[-4, -3, -7, 2, 4, 6]

7)

l1 = [1,-1,3]
l2 = [0,3,-2]
l3 = (lambda x,y :x+y)
l4 = map(l3,l1,l2)
print(list(l4))

[1, 2, 1]

You might also like