Wednesday, February 9, 2022

More on Lambda functions

The answer to the assignment from previous post is

addFunc = lambda a,b: a+b

print(addFunc(5,2))

Now we have a list of countries as:

countries = ['India','Mauritius','France','Turkey','Kenya','Hungary']

Your task is to use lambda function to print the length of each string in the list countries. This is how you can do this -

print(list(map(lambda x: len(x),countries)))

Now let us see how to use sort() with lambda. The syntax for sort is as follows:

list.sort(key = None, reverse= False)

The sort method uses the process of comparing the items to sort the elements of a list. The lambda functions allow key to become more versatile.

Suppose we have the list of country names: 

countries = ['India','Mauritius','France','Turkey','Kenya','Hungary']

Now, if we want to sort these names alphabetically, we can use the procedure given as follows:

countries = ['India','Mauritius','France','Turkey','Kenya','Hungary']

countries.sort()

print(countries)

Or we can use the sort() function given as follows:

countries.sort(key = lambda x:x[0])

print(countries)

So basically, the lambda functions takes each element(x) and sorts with respect to first element of string.

Suppose we have a list of names, and we want to sort the names on the basis of surnames:

We can write:

>>> names = ['Mahatma Gandhi','Jawaharlal Nehru','Subhash Chandra bose','Rani Laxmi Bai','Chandra Shekhar Azaad','Sarojini Naidu']

>>>names.sort(key = lambda x:x.split()[-1])

>>> print(names)

['Chandra Shekhar Azaad', 'Rani Laxmi Bai', 'Mahatma Gandhi', 'Sarojini Naidu', 'Jawaharlal Nehru', 'Subhash Chandra bose']

Here, lambda x:x.split()[-1], x.spilt() breaks each element into individual words. The names (having two parts) is split into a list of two words, and the names having three parts are split into a list of three words. -1 is the index of the last element in the list. So, lambda x:x.split()[-1] is working on last word of each element in the list names. (Notice, the name ‘Subhash Chandra bose’ is placed at the end because the surname does not begin with capital ‘b’. Let’s change the surname to capital B and see the output.)

>>> names = ['Mahatma Gandhi','Jawaharlal Nehru','Subhash Chandra Bose','Rani Laxmi Bai','Chandra Shekhar Azaad','Sarojini Naidu']

>>> names.sort(key = lambda x:x.split()[-1])

>>> print(names)

['Chandra Shekhar Azaad', 'Rani Laxmi Bai', 'Subhash Chandra Bose', 'Mahatma Gandhi', 'Sarojini Naidu', 'Jawaharlal Nehru']

We can also get the same result using the sorted() function.

>>> names = ['Mahatma Gandhi','Jawaharlal Nehru','Subhash Chandra Bose','Rani Laxmi Bai','Chandra Shekhar Azaad','Sarojini Naidu']

>>> print(list(sorted(names, key = lambda x:x.split()[-1])))

['Chandra Shekhar Azaad', 'Rani Laxmi Bai', 'Subhash Chandra Bose', 'Mahatma Gandhi', 'Sarojini Naidu', 'Jawaharlal Nehru']

The difference between the sort() and the sorted() function is that the sort() function modifies the list whereas the sorted() function provides a new list having the sorted values.


Share:

0 comments:

Post a Comment