Five Advanced Python Features and Their Usage
This article introduces five advanced Python features—lambda functions, map, filter, the itertools module, and generator functions—explaining their concepts, typical use cases, and providing clear code examples to demonstrate how they simplify data processing and improve memory efficiency.
Python offers many powerful built‑in features that become apparent through exploration and practical use. This tutorial presents five such advanced features, each accompanied by concise explanations and runnable code examples.
Lambda Functions
A lambda is an anonymous, inline function useful for simple expressions. It can take any number of arguments but must contain a single expression.
x = lambda a, b: a * b
print(x(5, 6)) # prints 30
x = lambda a: a * 3 + 3
print(x(3)) # prints 12Map Function
map() applies a given function to each element of an iterable, returning a map object that can be converted to a list.
def square_it_func(a):
return a * a
x = map(square_it_func, [1, 4, 7])
print(list(x)) # prints [1, 16, 49]
def multiplier_func(a, b):
return a * b
x = map(multiplier_func, [1, 4, 7], [2, 5, 8])
print(list(x)) # prints [2, 20, 56]Filter Function
filter() also applies a function to an iterable but retains only items for which the 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 odd numbers
def filter_odd_numbers(num):
if num % 2 == 0:
return True
else:
return False
filtered_numbers = filter(filter_odd_numbers, numbers)
print(list(filtered_numbers)) # [2, 4, 6, 8, 10, 12, 14]Itertools Module
The itertools library provides a collection of tools for efficient iterator manipulation, often replacing verbose loops or list comprehensions.
from itertools import *
# Joining two lists into a list of tuples
for i in izip([1, 2, 3], ['a', 'b', 'c']):
print(i)
# (1, 'a')
# (2, 'b')
# (3, 'c')
# Count produces an infinite iterator of consecutive integers
for i in izip(count(1), ['Bob', 'Emily', 'Joe']):
print(i)
# (1, 'Bob')
# (2, 'Emily')
# (3, 'Joe')
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)
# Checking: 2
# Checking: 4
# Result: 6
# Result: 8
# Result: 10
# Result: 12
a = sorted([1,2,1,3,2,1,2,3,4,5])
for key, group in groupby(a):
print(key, list(group))
# 1 [1, 1, 1]
# 2 [2, 2, 2]
# 3 [3, 3]
# 4 [4]
# 5 [5]Generator Functions
Generators produce values lazily, yielding one item at a time, which dramatically reduces memory consumption for large datasets.
# (1) Using a for loop
numbers = []
for i in range(1000):
numbers.append(i + 1)
total = sum(numbers)
# (2) Using a generator function
def generate_numbers(n):
num = 1
while num <= n:
yield num
num += 1
total = sum(generate_numbers(1000))
# (3) range() vs xrange() (Python 2)
# range creates a list in memory, whereas xrange returns a generator‑like object
total_range = sum(range(1001))
# total_xrange = sum(xrange(1001)) # Python 2 onlyThese examples illustrate how choosing the appropriate built‑in feature—whether a lambda, map, filter, itertools tool, or generator—can lead to cleaner code and more efficient memory usage, especially when processing large collections or working on memory‑constrained devices.
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.