Fundamentals 8 min read

Discover 5 Powerful Python Features You Might Be Missing

This article introduces five advanced Python features—lambda, map, filter, itertools, and generator functions—explaining their purpose, showing concise code examples, and demonstrating how they enable cleaner, more memory‑efficient programming.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Discover 5 Powerful Python Features You Might Be Missing

Advanced features of any programming language are often discovered through extensive use, such as stumbling upon elegant Python solutions while solving problems on Stack Overflow.

Below are five advanced Python features and their usage.

Lambda Functions

A lambda is a small anonymous function without a name, 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 12

This shows how to perform simple calculations without defining a full function.

Map Function

The built‑in map() applies a function to each element of an iterable (list, dict, etc.) in a clean, readable way.

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()

works like map() but returns only the elements for which the supplied function returns True.

# Our numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

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 module provides tools for iterator manipulation, enabling operations that would otherwise require multiple lines or complex comprehensions.

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)  # (1, 'a'), (2, 'b'), (3, 'c')

# count() generates an infinite sequence of 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)

# groupby() groups consecutive identical elements
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

Generator functions behave like iterators and can be used in for loops, saving memory by producing items on‑demand.

# Using a for loop
numbers = []
for i in range(1000):
    numbers.append(i+1)
total = sum(numbers)

# Using a generator
def generate_numbers(n):
    num = 1
    numbers = []
    while num < n:
        numbers.append(num)
        num += 1
    return numbers

total = sum(generate_numbers(1000))

# range() vs xrange()
total = sum(range(1000 + 1))
# In Python 2, xrange() would be used similarly

These features—lambda, map, filter, itertools, and generators—illustrate Python’s expressive power and help write concise, memory‑efficient code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LambdaMAPfilterGeneratorsAdvanced Features
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.