12 Python Built-in Functions I Wish I Knew Earlier!

Discover 12 built-in functions that can make your coding life easier and more efficient.

12 Python Built-in Functions I Wish I Knew Earlier!
Photo by Eduard Delputte on Unsplash

UNLOCK PYTHON’S HIDDEN POWER!

12 Python Built-in Functions I Wish I Knew Earlier!

Python is packed with powerful built-in functions that can save time, reduce code complexity, and improve performance.

Many of these functions remain under-utilized, especially by beginners who often reinvent the wheel without realizing that Python already provides efficient solutions.

Here are 12 built-in Python functions that I wish I had discovered earlier in my journey!

1. enumerate() — Keep Track of Indexes

Instead of using a counter variable while looping through a list, enumerate() lets you iterate with both the index and value:

fruits = ['apple', 'banana', 'cherry'] 
for index, fruit in enumerate(fruits, start=1): 
    print(index, fruit)

Output:

1 apple   
2 banana   
3 cherry

This eliminates the need for manually maintaining an index variable!


2. zip() — Pair Up Data Easily

When you need to iterate over multiple lists in parallel, zip() is a lifesaver:

names = ['John', 'David', 'Sam'] 
scores = [85, 90, 78] 
 
for name, score in zip(names, scores): 
    print(f"{name} scored {score}")

Output:

John scored 85   
David scored 90   
Sam scored 78

3. any() and all() – Check Conditions Cleanly

any() returns True if at least one element in an iterable is True, while all() returns True if all elements are True:

nums = [0, 1, 2, 3] 
print(any(nums))  # True (since there's at least one non-zero value) 
print(all(nums))  # False (since 0 is present)

It is much cleaner than writing explicit loops for condition checks!


4. sorted() with key – Custom Sorting in One Line

Instead of writing complex sorting logic, sorted() allows easy customization:

people = [('John', 25), ('David', 30), ('Sam', 20)] 
sorted_people = sorted(people, key=lambda x: x[1])  # Sort by age 
print(sorted_people)

Output:

[('Sam', 20), ('John', 25), ('David', 30)]

5. map() – Apply a Function to Every Element

Instead of using for loops for transformations, map() applies a function to all elements in an iterable:

nums = [1, 2, 3, 4] 
squared = list(map(lambda x: x ** 2, nums)) 
print(squared)

Output:

[1, 4, 9, 16]

Alternatively, use a list comprehension:

squared = [x ** 2 for x in nums]

6. filter() – Extract Elements That Meet a Condition

filter() helps to keep only the elements that satisfy a given condition:

nums = [10, 15, 20, 25, 30] 
evens = list(filter(lambda x: x % 2 == 0, nums)) 
print(evens)

Output:

[10, 20, 30]

7. reduce() – Accumulate Values Over an Iterable

The reduce() function (from functools) can be used to perform cumulative operations like sum, product, etc.:

from functools import reduce 
nums = [1, 2, 3, 4] 
product = reduce(lambda x, y: x * y, nums) 
print(product)

Output:

24

While sum() is more readable for summing numbers, reduce() shines in custom accumulations!


8. setdefault() – Get and Set Dictionary Values Simultaneously

Instead of using if key in dict checks, setdefault() initializes a dictionary key if it doesn’t exist:

student = {'name': 'John'} 
age = student.setdefault('age', 25)  # Sets default age if not present 
print(student)

Output:

{'name': 'John', 'age': 25}

9. vars() – Inspect Object Attributes

vars() returns the __dict__ of an object, making it easy to inspect its attributes dynamically:

class Student: 
    def __init__(self, name, age): 
        self.name = name 
        self.age = age 
 
john = Student('John', 25) 
print(vars(john))

Output:

{'name': 'John', 'age': 25}

10. globals() and locals() – Inspect Global & Local Variables

Want to inspect variables in the current scope? Use:

x = 10 
print(globals())  # Shows all global variables 
print(locals())   # Shows local variables within a function

11. isinstance() – Type Checking the Pythonic Way

Instead of using type() for type checking, isinstance() is more flexible:

x = 5 
print(isinstance(x, int))  # True 
print(isinstance(x, (int, float)))  # True (supports multiple types)

12. callable() – Check If an Object Is Callable

Want to check if an object can be called like a function? Use callable():

def greet(): 
    return "Hello!" 
 
print(callable(greet))  # True 
print(callable(42))     # False

Very useful when working with dynamically created objects!


Final Thoughts

These built-in functions have made my Python journey smoother, more efficient, and enjoyable. If you’re not using them yet, start incorporating them into your code — you’ll be surprised at how much cleaner and faster your scripts become!

Did I miss any of your favorite built-in functions? Let me know in the comments!