Learn Lambda and Make Your Program Pythonic

Learn Lambda and Make Your Program Pythonic

Lambda is a high-level usage in Python. To write code in Pythonic you need to understand these high-level usage. If you want to say that you are a real Python programmer, you must first write the code in Pythonic.

Lambda is a high-level usage in Python. To write code in Pythonic, you need to understand these high-level usage. If you want to say that you are a real Python programmer, you must first write the code in Pythonic.

Let ’s talk about Lambda usage today and write a short usage note.

Lambda means anonymous function. In the program, we may only use some simple functions once or twice. In this case, you don't need to write it as a function. Instead, you use Lambda to make the program more concise.

Lambda's writing prototype is: lambda Parameter: operation (parameter)

The left side of the colon is the parameter passed in, and the right side of the colon is the operation to use the parameter.

such as

lambda x:x+1

The above is equivalent to the following, add 1 to the parameter and return:

def plus(x):
return x+1
such as
lambda x,y:x*y

The above is equivalent to:

def mul(x,y):
retuen x*y

Give a few practical examples: 1. For example, to sort the list, the following simple list nums = [3, 1, 7, 4, 8, 10] sort nums in descending order, this is very simple, right:

>>>nums = [3, 1, 7, 4, 8, 10]>>>sorted(nums, reverse=True)>>>[10, 8, 7, 4, 3, 1]

What about sorting a nested list? For example: nums = [[10, 20, 11], [3, 9, 6], [8, 14, 3]] We want to sort in descending order by the second value in each element

You can't sort directly using the sorted wording above. At this time, it is very convenient to use lambda:

>>>nums = [[10, 20, 11], [3, 9, 6], [8, 14, 3]]
>>>sorted(nums, key=lambda x:x[1], reverse=True)
>>>[[10, 20, 11], [8, 14, 3], [3, 9, 6]]

The nested list is sorted in descending order according to the second value of the element. The lambda means to return the second value of the element. It is written as follows:

>>>def lis(x):
>>> return x[1]
>>>sorted(nums, key=lis, reverse=True)
>>>[[10, 20, 11], [8, 14, 3], [3, 9, 6]]

By comparison, writing code with lambda is a lot more introduction.

2. Another example is to sort in descending order by dictionary value

nums = {'yuan':80, 'ren':93, 'xue':60}
nums = {'yuan':80, 'ren':93, 'xue':60}
sorted(nums, key=lambda x: nums[x], reverse=True)

3. For example, if you want to take the maximum value in the list nums = ['1', '100', '111', '2', 2, 2.57] but there are strings in the list, you cannot directly compare the size, how to do it, Use lambda for processing.

>>>nums = ['1', '100', '111', '2', 2, 2.57]
>>>max(nums, key=lambda x:int(x))
>>>'111'

This writing is equivalent to

def trsn(x):
return int(x)
max(nums, key=trsn)

If lambda is combined with higher-level Python syntax such as map and filter, there are more uses.

The advantage of using a lambda as above is to make the code more concise, which is a programmer's pursuit of code. The disadvantage is that the program is not easy to understand, but as long as you understand the usage of lambda, it is not so difficult to understand.

python lambda pythonic

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How to Debug Local Lambda Python functions for Beginners

While developing your lambda functions, debugging may become a problem. As a person who benefits a lot from step-by-step debugging, I had difficulty debugging lambda functions. I got lost in the logs. Redeploying and trying again with different parameters over and over... then, I found the AWS Serverless Application Model (SAM) Command Line Interface (CLI). The AWS SAM CLI lets you debug your AWS Lambda functions in a good, old, step-by-step way.

Python Lambda Functions

Python Lambda Functions. Python lambda functions are an invaluable feature. One of the benefits of using Python is that it facilitates a relatively compact code structure compared to other programming languages. This means getting so much more done with fewer lines of code. One such tool is the lambda function. These allow us to create anonymous functions in python.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.