Built-in Functions
Overview
Utilize built-in functions (len, map, filter, zip, sorted, enumerate) to write clean, fast code.
Learning Objectives
- Use zip and enumerate to process elements in loops.
- Apply map and filter transforms on collections.
- Understand sorted list sorting protocols.
Concept Explanation
Built-in functions are implemented in native C, which makes them execute much faster than custom Python loops. enumerate tracks index values. zip merges iterables. any and all check truthiness. sorted returns a new sorted list.
Code Examples
Example 1 — Basics
This example introduces the fundamental syntax and concepts.
print(len([1, 2, 3]))
print(sum([1, 2]))
Example 2 — Everyday Usage
This example demonstrates a realistic scenario handling business parameters.
names = ['Alice', 'Bob']
# Zip two collections into dictionary
roles = ['Admin', 'Editor']
registry = dict(zip(names, roles))
print(registry)
Example 3 — Advanced Example
This example shows clean, production-grade code structure following senior development standards.
# Custom sorted key logic using built-in functions
users = [
{'name': 'Alex', 'score': 90},
{'name': 'Bob', 'score': 95},
{'name': 'Charlie', 'score': 85}
]
# Sort by score descending
sorted_users = sorted(users, key=lambda u: u['score'], reverse=True)
print(sorted_users)
Visual Flow
The following execution flow represents the step-by-step evaluation inside the interpreter:
Input collections → Call built-in → Zip iterators → Yield merged tuples lazily
Common Mistakes
Review these common pitfalls when working with this topic:
- Forgetting map and filter return lazy iterators that must be cast (e.g. using list()) to show values.
- Shadowing built-in function names by declaring local variables with the same names (e.g. sum = 0).
- Using complex loop lookups instead of zip or enumerate helpers.
- Not utilizing key parameter functions in sorted() lookups.
Best Practices
Enforce these Pythonic best practices in your codebase:
idx = 0
for x in items:
print(idx, x)
idx += 1
for idx, x in enumerate(items):
print(idx, x) # Clean index tracking
Performance Notes
Keep these optimization guidelines in mind for performance-sensitive hotpaths:
- Built-in functions are compiled in C, executing much faster than raw python code loops.
- any() and all() use short-circuit logic, returning early as soon as the result is determined.
- sorted() uses TimSort, which has O(N log N) worst-case time complexity.
Quick Revision
Use these key summaries for last-minute revision:
- Built-ins are implemented in C.
- enumerate() tracks index values.
- zip() merges parallel iterables.
- sorted() returns a new sorted list.
- any() and all() check truthiness.
- Do not shadow built-in function names.
DataPlayArena Technical Editorial Board
Curated & maintained by enterprise data platform engineers with 10+ years in distributed systems.
Runtime Verification Standards: Every code example, query, and architecture diagram in this tutorial is compiled and verified against real runtimes (Apache Spark 3.5, Python 3.11, PostgreSQL, and DuckDB). If you spot any errata or want to suggest improvements, please submit feedback.