Unlock Python’s Hidden Gems: Essential Built‑in Functions Every Developer Should Know
This article introduces a curated collection of Python’s most useful built‑in functions and standard‑library modules—including all, any, argparse, Counter, defaultdict, dataclass, datetime, lru_cache, itertools.chain, json, pickle, pprint, re, timeit, and uuid—explaining their purpose, providing clear usage examples, and highlighting practical scenarios where they simplify everyday coding tasks.
1. all - Check if all elements satisfy a condition
Function Overview
The
allfunction returns
Trueif every element of an iterable meets the given condition; it returns
Truefor an empty iterable.
Usage Example
Check if all numbers in a list are positive:
<code>numbers = [1, 2, 3, 4]
result = all(num > 0 for num in numbers)
print(result) # True</code>Check if all characters in a string are alphabetic:
<code>text = "Hello"
result = all(char.isalpha() for char in text)
print(result) # True</code>Check if all dictionary values are greater than 10:
<code>data = {'a': 11, 'b': 12, 'c': 9}
result = all(value > 10 for value in data.values())
print(result) # False</code>Typical Use Cases
Data validation – ensure every item meets a rule before processing.
Conditional checks – quickly verify uniformity across a collection.
2. any - Check if any element satisfies a condition
Function Overview
The
anyfunction returns
Trueif at least one element of an iterable meets the condition; otherwise it returns
False. An empty iterable yields
False.
Usage Example
Check if a list contains a number greater than 10:
<code>numbers = [1, 5, 8, 12]
result = any(num > 10 for num in numbers)
print(result) # True</code>Check if a string contains the character 'h':
<code>text = "hello"
result = any(char == 'h' for char in text)
print(result) # True</code>Check if a dictionary has a None value:
<code>data = {'name': 'Alice', 'age': None}
result = any(value is None for value in data.values())
print(result) # True</code>Typical Use Cases
Early exit checks – stop processing as soon as a match is found.
Validation – detect the presence of invalid or special values.
3. argparse - Command‑line argument parsing
Function Overview
The
argparsemodule creates user‑friendly command‑line interfaces, automatically generating help messages and handling argument types, defaults, and required flags.
Usage Example
Basic argument parsing:
<code>import argparse
parser = argparse.ArgumentParser(description="Demo script")
parser.add_argument('--name', type=str, help='Your name')
args = parser.parse_args()
print(f"Hello, {args.name}!")
</code>Required and default arguments:
<code>parser.add_argument('--age', type=int, required=True, help='Your age')
parser.add_argument('--city', type=str, default='Unknown', help='City')
</code>Boolean flag:
<code>parser.add_argument('--verbose', action='store_true', help='Enable verbose mode')
</code>Typical Use Cases
Scripting – turn Python scripts into flexible command‑line tools.
Automation – pass parameters without modifying source code.
4. collections.Counter - Counting hashable objects
Function Overview
Counteris a dict subclass that tallies the number of occurrences of each element in an iterable.
Usage Example
Count characters in a string:
<code>from collections import Counter
text = "hello world"
counter = Counter(text)
print(counter) # Counter({'l': 3, 'o': 2, ...})
</code>Count items in a list:
<code>items = ['apple', 'banana', 'apple']
counter = Counter(items)
print(counter) # Counter({'apple': 2, 'banana': 1})
</code>Most common elements:
<code>most_common = counter.most_common(2)
print(most_common) # [('apple', 2), ('banana', 1)]
</code>Typical Use Cases
Frequency analysis – text processing, data summarization.
Finding top‑N items – quick extraction of most common elements.
5. collections.defaultdict - Dictionaries with default values
Function Overview
defaultdictreturns a default value for missing keys, avoiding
KeyErrorchecks.
Usage Example
Simple counting:
<code>from collections import defaultdict
dd = defaultdict(int)
dd['a'] += 1
print(dd) # defaultdict(<class 'int'>, {'a': 1})
</code>Group items by length:
<code>words = ['apple', 'banana', 'pear']
by_len = defaultdict(list)
for w in words:
by_len[len(w)].append(w)
print(by_len) # defaultdict(<class 'list'>, {5: ['apple', 'pear'], 6: ['banana']})
</code>Typical Use Cases
Aggregations – building histograms or grouped collections.
Simplifying code – no need for explicit existence checks.
6. dataclasses.dataclass - Lightweight data classes
Function Overview
The
@dataclassdecorator auto‑generates
__init__,
__repr__, and comparison methods, reducing boilerplate for classes that primarily store data.
Usage Example
Simple data class:
<code>from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
p = Person(name='Alice', age=30)
print(p) # Person(name='Alice', age=30)
</code>Frozen (immutable) data class:
<code>@dataclass(frozen=True)
class Point:
x: int
y: int
</code>Typical Use Cases
Model objects – representing entities such as users or configurations.
Immutable records – safe data transfer without accidental mutation.
7. datetime - Date and time handling
Function Overview
The
datetimemodule provides classes for manipulating dates, times, and timedeltas.
Usage Example
Current timestamp:
<code>from datetime import datetime
now = datetime.now()
print(f"Current time: {now}")
</code>Formatting:
<code>formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)
</code>Parsing a string:
<code>dt = datetime.strptime("2024-09-07 15:32:18", "%Y-%m-%d %H:%M:%S")
print(dt)
</code>Timedelta arithmetic:
<code>from datetime import timedelta
future = now + timedelta(days=10)
print(future)
</code>Typical Use Cases
Logging – timestamps for events.
Scheduling – calculate future dates or durations.
Data parsing – convert string dates to objects for analysis.
8. functools.lru_cache - Caching function results
Function Overview
The
@lru_cachedecorator caches recent function calls, avoiding repeated expensive computations.
Usage Example
Memoized Fibonacci:
<code>from functools import lru_cache
@lru_cache(maxsize=128)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
print(fib(100))
</code>Cache statistics:
<code>fib.cache_info()
</code>Clear cache:
<code>fib.cache_clear()
</code>Typical Use Cases
Recursive algorithms – dynamic programming.
IO‑bound functions – cache results of expensive lookups.
9. itertools.chain - Concatenating iterables
Function Overview
chainjoins multiple iterables into a single iterator without creating intermediate lists.
Usage Example
Chain two lists:
<code>from itertools import chain
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list(chain(list1, list2))
print(result) # [1, 2, 3, 4, 5, 6]
</code>Flatten a list of lists:
<code>nested = [[1, 2], [3, 4]]
flat = list(chain.from_iterable(nested))
print(flat) # [1, 2, 3, 4]
</code>Typical Use Cases
Merging data streams – process several sources as one.
Flattening nested structures efficiently.
10. json - JSON serialization and deserialization
Function Overview
The
jsonmodule converts between Python objects and JSON strings/files.
Usage Example
Object to JSON string:
<code>import json
data = {'name': 'John', 'age': 30}
json_str = json.dumps(data)
print(json_str)
</code>JSON string to object:
<code>obj = json.loads(json_str)
print(obj['name'])
</code>Write JSON to file:
<code>with open('data.json', 'w') as f:
json.dump(data, f)
</code>Read JSON from file:
<code>with open('data.json') as f:
data = json.load(f)
print(data)
</code>Typical Use Cases
Web APIs – exchange data between client and server.
Configuration files – store settings in a readable format.
11. pickle - Object serialization
Function Overview
pickleserializes arbitrary Python objects to a byte stream and restores them later.
Usage Example
Save object to file:
<code>import pickle
data = {'a': 1, 'b': 2}
with open('data.pkl', 'wb') as f:
pickle.dump(data, f)
</code>Load object from file:
<code>with open('data.pkl', 'rb') as f:
data = pickle.load(f)
print(data)
</code>Serialize to bytes:
<code>byte_stream = pickle.dumps(data)
print(byte_stream)
</code>Typical Use Cases
Persisting complex Python objects.
Inter‑process communication in Python‑only environments.
12. pprint - Pretty‑print data structures
Function Overview
pprintformats nested containers (lists, dicts, etc.) for readable console output.
Usage Example
Print a nested dictionary:
<code>from pprint import pprint
data = {'name': 'Alice', 'address': {'city': 'Wonderland', 'street': '123 Main St'}, 'hobbies': ['reading', 'hiking']}
pprint(data)
</code>Typical Use Cases
Debugging complex data structures.
Logging readable snapshots of program state.
13. re - Regular expressions
Function Overview
The
remodule provides pattern matching, searching, substitution, and splitting capabilities for strings.
Usage Example
Match digits at the start:
<code>import re
m = re.match(r'\d+', '123abc')
print(m.group()) # 123
</code>Search for a word:
<code>m = re.search(r'[a-z]+', '123abc456')
print(m.group()) # abc
</code>Find all numbers:
<code>nums = re.findall(r'\d+', '123abc456def789')
print(nums) # ['123', '456', '789']
</code>Replace digits with '#':
<code>new = re.sub(r'\d+', '#', '123abc456')
print(new) # #abc#
</code>Split on digits:
<code>parts = re.split(r'\d+', 'abc123def456ghi')
print(parts) # ['abc', 'def', 'ghi']
</code>Named groups for a date:
<code>pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
m = re.search(pattern, 'Date: 2024-09-07')
print(m.group('year'), m.group('month'), m.group('day'))
</code>Typical Use Cases
Input validation – emails, phone numbers, etc.
Data extraction – pull dates, IDs, or amounts from text.
Log parsing – isolate timestamps or error codes.
14. timeit.timeit - Measuring execution time
Function Overview
timeit.timeitruns a statement repeatedly and returns the total time, useful for micro‑benchmarks.
Usage Example
Simple timing:
<code>import timeit
elapsed = timeit.timeit('sum(range(100))', number=10000)
print(f"Execution time: {elapsed} seconds")
</code>Timing a function:
<code>def work():
return sum(range(100))
elapsed = timeit.timeit(work, number=10000)
print(elapsed)
</code>With setup code:
<code>setup = 'import random; data = [random.random() for _ in range(1000)]'
stmt = 'sorted(data)'
elapsed = timeit.timeit(stmt, setup=setup, number=1000)
print(elapsed)
</code>Typical Use Cases
Performance profiling – compare alternative implementations.
Optimization – identify bottlenecks in critical code paths.
15. uuid - Generating unique identifiers
Function Overview
The
uuidmodule creates universally unique identifiers (UUIDs) using several algorithms.
Usage Example
Time‑based UUID (v1):
<code>import uuid
uid = uuid.uuid1()
print(uid)
</code>Random UUID (v4):
<code>uid = uuid.uuid4()
print(uid)
</code>Name‑based UUID (v3):
<code>uid = uuid.uuid3(uuid.NAMESPACE_DNS, 'example.com')
print(uid)
</code>SHA‑1 name‑based UUID (v5):
<code>uid = uuid.uuid5(uuid.NAMESPACE_URL, 'http://example.com')
print(uid)
</code>Convert to string:
<code>uid_str = str(uuid.uuid4())
print(uid_str)
</code>Typical Use Cases
Unique keys for databases or distributed systems.
Session identifiers and token generation.
Tracking objects across services without collisions.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.