5 Powerful Python Features You Might Not Be Using (and How to Apply Them)
Discover five advanced Python features—including lambda, map, filter, itertools, and generator functions—explained with clear examples, showing how they simplify code, improve performance, and enable more elegant data processing for both small scripts and memory‑constrained applications.
Python is a beautiful language: simple yet powerful. This article highlights five advanced features that many developers overlook and demonstrates how to use them effectively.
Lambda Functions
A lambda is a small anonymous function without a name, useful for simple expressions.
x = lambda a, b: a * b
print(x(5, 6)) # prints '30'
x = lambda a: a*3 + 3
print(x(3)) # prints '12'Lambda functions can accept any number of arguments, but their body must be a single expression.
Map Function
map()applies a given function to each element of an iterable (list, dict, etc.), providing a clean and readable way to transform data.
def square_it_func(a):
return a * a
x = map(square_it_func, [1, 4, 7])
print(x) # prints '[1, 16, 49]'
def multiplier_func(a, b):
return a * b
x = map(multiplier_func, [1, 4, 7], [2, 5, 8])
print(x) # prints '[2, 20, 56]'Filter Function
filter()works like map() but returns only the elements for which the provided function returns True.
# Our numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# Function that filters out all numbers which are odd
def filter_odd_numbers(num):
if num % 2 == 0:
return True
else:
return False
filtered_numbers = filter(filter_odd_numbers, numbers)
print(filtered_numbers) # filtered_numbers = [2, 4, 6, 8, 10, 12, 14]The filter() function evaluates each element and keeps only those that satisfy the condition.
Itertools Module
The itertools module provides a collection of tools for handling iterators, enabling complex operations with concise code.
from itertools import *
# Easy joining of two lists into a list of tuples
for i in izip([1, 2, 3], ['a', 'b', 'c']):
print(i) # ('a', 1) ('b', 2) ('c', 3)
# The count() function returns an iterator that produces consecutive integers forever.
for i in izip(count(1), ['Bob', 'Emily', 'Joe']):
print(i) # (1, 'Bob') (2, 'Emily') (3, 'Joe')
# dropwhile() returns an iterator that skips elements until the condition becomes false.
def check_for_drop(x):
print('Checking: ', x)
return x > 5
for i in dropwhile(check_for_drop, [2, 4, 6, 8, 10, 12]):
print('Result: ', i)
# groupby() groups consecutive identical elements.
a = sorted([1, 2, 1, 3, 2, 1, 2, 3, 4, 5])
for key, value in groupby(a):
print(key, list(value))Generator Functions
Generator functions behave like iterators and can be used in for loops, reducing memory usage compared to building full lists.
# (1) Using a for loop
numbers = []
for i in range(1000):
numbers.append(i + 1)
total = sum(numbers)
# (2) Using a generator
def generate_numbers(n):
num, numbers = 1, []
while num < n:
numbers.append(num)
num += 1
return numbers
total = sum(generate_numbers(1000))
# (3) range() vs xrange()
total = sum(range(1000 + 1))
# In Python 2, xrange() would generate numbers lazily.Generators are ideal when processing very large sequences or when memory is limited, such as on mobile devices or edge computing platforms.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
