How to sort With Lambda in Python

Hello guys. How are you all. In this tutorial we will learn about How to sort With Lambda in Python. In python there are many ways to sort out the list. Here we will discuss about lambda method. By using lambda function you can sort a list by two ways. which are given in below methods. With lambda function we will use sorted() function also. We will also use one parameter in this function which is key parameter. which is allow us our own sorting methods. We can skip some words and then sort our list by our own method. Lets learn this how it all works by given below methods.

How to sort With Lambda in Python

  1. Sort With Lambda in Python

    to sort With Lambda in Python just use this method. In this method we will learn about how to sort With Lambda in Python. We can sort a list by using so many methods. Here we will use lambda to sort a list in python. In this method we have the list with prefixed word “page”. Now we will make sure that the word not cosider during sorting the list. With the use of sorted() function we will use lambda and sort the list. So lets learn this by given below example. I hope you guys like it. mylst = ['page16','page9','page26','page8','page17','page5',] sortedlist = sorted(mylst, key=lambda x: int(x[4:])) print(sortedlist) Output : ['page5', 'page8', 'page9', 'page16', 'page17', 'page26']

  2. How to sort With Lambda in Python

    to sort With Lambda in Python just use this method. In this method we will use a list of a tuple to sort a list by using lambda. Here i have make a list of tuple where i used name and the number as a marks. So here we specify the function to sort a list on the basis of the second element. It means it will sorted out on the basis of number. So lets learn this by given below example. Its very easy and simple method. Lets learn this. mylst = [('happy',45),('Jiya',35),('roshani',47),('Sakshi',44)] sortedlist = sorted(mylst, key=lambda x: x[1]) print(sortedlist)
    Output :
    [('Jiya', 35), ('Sakshi', 44), ('happy', 45), ('roshani', 47)]

Method : 1

In this method we will learn about how to sort With Lambda in Python. We can sort a list by using so many methods. Here we will use lambda to sort a list in python. In this method we have the list with prefixed word “page”. Now we will make sure that the word not cosider during sorting the list. With the use of sorted() function we will use lambda and sort the list. So lets learn this by given below example. I hope you guys like it.

mylst = ['page16','page9','page26','page8','page17','page5',]
sortedlist = sorted(mylst, key=lambda x: int(x[4:]))
print(sortedlist)

Output :

['page5', 'page8', 'page9', 'page16', 'page17', 'page26']

Method : 2

In this method we will use a list of a tuple to sort a list by using lambda. Here i have make a list of tuple where i used name and the number as a marks. So here we specify the function to sort a list on the basis of the second element. It means it will sorted out on the basis of number. So lets learn this by given below example. Its very easy and simple method. Lets learn this.

mylst = [('happy',45),('Jiya',35),('roshani',47),('Sakshi',44)]
sortedlist = sorted(mylst, key=lambda x: x[1])
print(sortedlist)

Output :

[('Jiya', 35), ('Sakshi', 44), ('happy', 45), ('roshani', 47)]

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment