Lambda functions, comprehensions, generators, decorators, and hashability.When you have developed a good understanding of basic data structures and their key functionalities, it’s time to explore some more advanced techniques in Python. In this article, I’d like to review five concepts that you can take advantage of in your code.
When you have developed a good understanding of basic data structures and their key functionalities, it’s time to explore some more advanced techniques in Python. In this article, I’d like to review five concepts that you can take advantage of in your code.
Lambda functions are also called anonymous functions in Python. Some people simply refer to them as lambdas. They have the following syntax: lambda arguments: expression
. In essence, we use the lambda keyword to signify the declaration of a lambda function. Then we list the arguments, the number of which can be zero or more. After the colon, we list the expression that uses these arguments for any applicable operations.
Lambda functions are particularly useful in cases where we need to have a short one-time use function. For instance, several built-in functions have the key argument, to which we can set a lambda function.
Sorting With a Lambda Function
In the above code, we wanted to sort a list of tuples. By default, the tuples will be sorted based on each of the items contained. In this case, the sorting was based on the names’ first letters. However, we wanted to solve by the scores, which are the second items of the tuples. To accomplish it, we took advantage of the lambda function, in which the x
argument refers to each tuple that was to be sorted. Because the score was the second item in each tuple, we just needed to specify the index of 1 to access the second item.
Probably the most Pythonic example that is mentioned a lot is the comprehension technique. In essence, this technique allows us to create a list, dictionary, or set using an exiting iterable, which are named list comprehension, dictionary comprehension, and set comprehension, respectively. The following code snippet shows you these usages.
Comprehensions
The syntax of these comprehensions looks similar. Here’s a quick highlight of the differential forms. It should be noted that you can add conditions to keep the items you need.
data-science programming python technology artificial-intelligence
🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...
Enroll in our Data Science with Python training in Chennai. Best Data Science with Python Training courses in Chennai for 100% Job Placements Support.
🔥Intellipaat Python for Data Science Course: https://intellipaat.com/python-for-data-science-training/In this python for data science video you will learn e...
Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now
Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...