Using Python Built In Functions
Python# Common built-ins in one glance numbers = [5, 3, 8, 1] len(numbers) # 4 sorted(numbers) # [1, 3, 5, 8] sum(numbers) # 17 max(numbers, default=0) # 8 list(enumerate(numbers)) # [(0, 5), (1, 3), (2, 8), (3, 1)] list(map(str, numbers)) # ['5', '3', '8', '1'] list(filter(lambda x: x > 3, numbers)) # [5, 8] list(zip(numbers, "abcd")) # [(5,'a'), (3,'b'), (8,'c'), (1,'d')]
When a task needs a "helper function," check if a built-in already solves it before writing custom code.
Progress:
- Step 1: Identify the operation category (iteration, conversion, inspection, I/O, math)
- Step 2: Match to the right built-in (see reference table below)
- Step 3: Check signature/edge cases (default args, generator vs list return)
- Step 4: Verify with a quick REPL test before embedding in code
Reference by Category
Type conversion / construction
int(), float(), str(), bool(), list(), tuple(), dict(), set(), frozenset(), bytes(), bytearray(), complex()
Iteration & sequence ops
enumerate(), zip(), map(), filter(), range(), reversed(), sorted(), iter(), next(), all(), any()
Inspection / introspection
type(), isinstance(), issubclass(), id(), hasattr(), getattr(), setattr(), delattr(), dir(), vars(), callable()
Math / numeric
abs(), round(), sum(), min(), max(), pow(), divmod()
Functional programming
map(), filter(), zip(), sorted(key=...), functools.reduce (not built-in, note it separately)
I/O
print(), input(), open()
Object protocol
len(), repr(), format(), hash(), iter(), next()
Scope / execution (use sparingly)
globals(), locals(), eval(), exec(), compile()
Example 1: Filtering + transforming without a loop Input:
Pythonwords = ["apple", "Banana", "cherry", "Date"] # Need: lowercase words only, capitalized
Output:
Pythonresult = [w.capitalize() for w in filter(str.islower, map(str.lower, words))] # or simply: result = [w.capitalize() for w in words if w[0].islower()]
Example 2: Checking type safely Input:
Pythondef process(value): if type(value) == list: # fragile ...
Output:
Pythondef process(value): if isinstance(value, list): # handles subclasses correctly ...
Example 3: Getting index + value together Input:
Pythonfor i in range(len(items)): print(i, items[i])
Output:
Pythonfor i, item in enumerate(items): print(i, item)
Example 4: Combining two lists into pairs Input:
Pythonnames = ["Alice", "Bob"] ages = [30, 25]
Output:
Pythonpaired = dict(zip(names, ages)) # {'Alice': 30, 'Bob': 25}
- Prefer
isinstance()overtype() ==for type checks (respects inheritance). - Use
enumerate()instead of manual index counters. - Use
sorted(iterable, key=..., reverse=...)instead of manual sort logic — never write bubble sort by hand. zip()stops at the shortest iterable; useitertools.zip_longestif lengths may differ.map()/filter()return iterators (lazy) in Python 3 — wrap inlist()to materialize.- Use
any()/all()for short-circuit boolean checks instead of manual loops with flags. - Give
min()/max()adefault=value when the iterable might be empty, to avoidValueError. - Avoid
eval()/exec()on untrusted input — they execute arbitrary code. - Use
open()with a context manager (with open(...) as f:) to ensure files close properly.
- Comparing types with
==instead ofisinstance()— breaks for subclasses. - Forgetting
map/filter/zip/rangeare iterators — printing them shows a generator object, not values; wrap withlist(). - Using
eval()for parsing data — useast.literal_eval()orjson.loads()instead. - Calling
max()/min()on an empty sequence withoutdefault=— raisesValueError. - Shadowing built-ins by naming variables
list,dict,str,len,type, etc. — silently breaks later code using the real built-in. - Using
input()return value as a number directly — it's always a string; cast withint()/float(). - Assuming
sorted()mutates in place — it returns a new list; use.sort()on the list itself for in-place mutation.