0% found this document useful (0 votes)
26 views4 pages

Assignment 5 Utkarsh

The document contains solutions to 6 questions involving the use of lambda functions, map functions, filter functions and reduce functions in Python. The questions involve sorting a list of tuples based on integer value, finding squares of numbers in a list, converting a list of integers to strings, computing product of a range of numbers, filtering numbers in a list divisible by 2 and 3, and finding palindromes in a list of strings.

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Assignment 5 Utkarsh

The document contains solutions to 6 questions involving the use of lambda functions, map functions, filter functions and reduce functions in Python. The questions involve sorting a list of tuples based on integer value, finding squares of numbers in a list, converting a list of integers to strings, computing product of a range of numbers, filtering numbers in a list divisible by 2 and 3, and finding palindromes in a list of strings.

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 5: Lambda, Map, Reduce, Filter - Utkarsh Gaikwad

Assignment pdf link

Question 1

Question 1: Create a python program to sort the given list of tuples


based on integer value using a lambda function.

[('Sachin Tendulkar', 34357), ('Ricky Ponting', 27483), ('Jack Kallis', 25534), ('Virat Kohli',
24936)]

Answer: I have also used sorted inbuilt function for this along with
lambda function
The sorted function is a built-in function in Python used to sort a sequence (lists, tuples, etc.) in ascending order
by default. The function returns a sorted list from the input sequence.

Syntax: sorted(iterable, key=None, reverse=False)

iterable: The sequence (list, tuple, etc.) that needs to be sorted.


key: A function that serves as a key for the sort comparison. It takes one argument and returns one value.
The default value is None, which sorts the elements based on their natural order.
reverse: A Boolean value that indicates whether the sort order should be in descending (True) or ascending
(False) order. The default value is False.

In [1]:
# Orignal list players is shown
players = [('Sachin Tendulkar', 34357), ('Ricky Ponting', 27483), ('Jack Kallis', 25534)
, ('Virat Kohli', 24936)]
print(players)

[('Sachin Tendulkar', 34357), ('Ricky Ponting', 27483), ('Jack Kallis', 25534), ('Virat K
ohli', 24936)]

In [2]:

# Sort based on sorted function where i use lambda function in key


sort_players = sorted(players, key = lambda x: x[1], reverse=False)
print(sort_players)

[('Virat Kohli', 24936), ('Jack Kallis', 25534), ('Ricky Ponting', 27483), ('Sachin Tendu
lkar', 34357)]

Question 2
Question 2

Question 2: Write a Python Program to find the squares of all the


numbers in the given list of integers using lambda and map
functions.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Answer :
In [3]:
l = [1,2,3,4,5,6,7,8,9,10]
sqr = map(lambda x : x*x , l)
print(list(sqr))

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Question 3

Question 3: Write a python program to convert the given list of integers


into a tuple of strings. Use map and lambda functions

Given String: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Expected output: ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')

Answer :
In [4]:
l = [1,2,3,4,5,6,7,8,9,10]
string_l = map(lambda s: str(s),l)
print(tuple(string_l))

('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')

Question 4

Question 4 : Write a python program using reduce function to compute


the product of a list containing numbers from 1 to 25.
Answer :
In [5]:
# Creating a list containing numbers from 1 to 25
nums = list(range(1,26))
print(nums)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
]

In [6]:
# Creating a reduce function with lambda for computing the product
from functools import reduce
product = reduce(lambda x,y: x*y,nums)
print(f'Product of numbers from 1 to 25 is {product}')

Product of numbers from 1 to 25 is 15511210043330985984000000

Question 5

Question 5 : Write a python program to filter the numbers in a given list


that are divisible by 2 and 3 using the filter function.

[2, 3, 6, 9, 27, 60, 90, 120, 55, 46]

Answer :
In [7]:

# Defining the orignal list and printing


l = [2, 3, 6, 9, 27, 60, 90, 120, 55, 46]
print(l)

[2, 3, 6, 9, 27, 60, 90, 120, 55, 46]

In [8]:
# Filtered list of numbers which are divisible by 2 and 3
filtered_l = filter(lambda x : x%2==0 and x%3==0,l)
print(list(filtered_l))

[6, 60, 90, 120]

Question 6

Question 6: Write a python program to find palindromes in the given list


of strings using lambda and filter function.

['python', 'php', 'aba', 'radar', 'level']


['python', 'php', 'aba', 'radar', 'level']

Answer :
Palindrome - a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam, nurses, run

In [9]:
# Defining orignal list of strings
s = ['python', 'php', 'aba', 'radar', 'level']
print(s)

['python', 'php', 'aba', 'radar', 'level']

In [10]:
# Using filter and lambda function to find palindromes
# I have done case unification to lower here incase any string written in different cases
palindromes = filter(lambda x: x.lower() == x[::-1].lower() , s)
print(list(palindromes))

['php', 'aba', 'radar', 'level']

You might also like