Pure Python Techniques: Lambda Functions, List Comprehensions, and Zip
This article introduces several powerful pure‑Python features—lambda functions, list comprehensions, and the zip function—demonstrating how they can simplify data cleaning, feature engineering, and iterative processing with concise code examples, helping programmers reduce reliance on external libraries while improving productivity.
Learning Pandas and NumPy is great, but before relying on external libraries you might want to explore the power of pure Python. This article presents three useful pure‑Python techniques that are frequently used during data preparation and feature engineering.
1. Lambda Functions
Lambda functions allow you to create anonymous, one‑line functions without the need for a full def definition. They are handy for simple operations such as squaring a number or checking if a value is even.
# regular function
def square_number(x):
res = x ** 2
return res
# lambda function
square = lambda x: x ** 2Both implementations produce the same result, but the lambda version is more compact.
# regular function
def is_even(x):
if x % 2 == 0:
return True
else:
return False
# lambda function
even = lambda x: x % 2 == 0
print('is_even(4): {}'.format(is_even(4)))
print('is_even(3): {}'.format(is_even(3)))
print('even(4): {}'.format(even(4)))
print('even(3): {}'.format(even(3)))The same logic is expressed in two ways; choose the style you prefer.
2. List Comprehensions
List comprehensions let you build lists in a single, readable line, effectively acting as an inline loop. They are especially useful for feature engineering tasks such as filtering items that start with a specific character.
lst = ['Acer', 'Asus', 'Lenovo', 'HP'] # regular function
def starts_with_a(lst):
valids = []
for word in lst:
if word[0].lower() == 'a':
valids.append(word)
return valids
# list comprehension
lst_comp = [word for word in lst if word[0].lower() == 'a']
print('starts_with_a: {}'.format(starts_with_a(lst)))
print('list_comprehension: {}'.format(lst_comp))While the regular function is more verbose, the list comprehension achieves the same result with less code.
3. Zip Function
The built‑in zip function lets you iterate over multiple sequences in parallel, which is handy when you need to compare or combine corresponding elements, such as calculating differences between two sales series.
sales_north = [350, 287, 550, 891, 241, 653, 882]
sales_south = [551, 254, 901, 776, 105, 502, 976]
for s1, s2 in zip(sales_north, sales_south):
print(s1 - s2)You can even zip three or more sequences by adding additional arguments.
Conclusion
Pure Python offers powerful tools that can replace many external libraries for everyday data‑science tasks. Mastering lambda functions, list comprehensions, and zip will make you a more efficient programmer and help you write cleaner, faster code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
