5 Python Built-in Function That Will Make You a Better Coder
When I first started learning Python, I focused mostly on syntax and solving basic problems. But as I wrote more code, I realized that some of Python’s built-in functions could make my code cleaner, faster, and easier to understand. These aren’t just time-savers—they’re game-changers. In this post, I’m sharing 5 Python built-in functions that have genuinely helped me become a better coder. Whether you’re just getting started or looking to refine your skills, these functions are worth knowing. 1. enumerate() – Stop Using Range with Len One of the most common anti-patterns in Python is using range(len(items)) to get both indices and values when iterating over a list. The enumerate() function provides a much cleaner solution. Instead of this: items = [‘apple’, ‘banana’, ‘cherry’] for i in range(len(items)): print(f”{i}: {items[i]}”) Write this: items = [‘apple’, ‘banana’, ‘cherry’] for i, item in enumerate(items): print(f”{i}: {item}”) The enumerate() function returns pairs of (index, value) for each item in an iterable. You can even specify a starting value for the index: for i, item in enumerate(items, start=1): print(f”{i}: {item}”) # Starts counting from 1 This approach is more readable, less error-prone, and slightly more performant than the manual indexing approach. 2. zip() – Iterate Multiple Sequences Simultaneously When you need to process multiple lists or sequences together, zip() it is your best friend. It pairs up elements from multiple iterables, making parallel iteration elegant and intuitive. Instead of this: names = [‘Alice’, ‘Bob’, ‘Charlie’] ages = [25, 30, 35] cities = [‘New York’, ‘London’, ‘Tokyo’] for i in range(len(names)): print(f”{names[i]} is {ages[i]} years old and lives in {cities[i]}”) Write this: names = [‘Alice’, ‘Bob’, ‘Charlie’] ages = [25, 30, 35] cities = [‘New York’, ‘London’, ‘Tokyo’] for name, age, city in zip(names, ages, cities): print(f”{name} is {age} years old and lives in {city}”) The zip() function stops when the shortest iterable is exhausted. If you need to handle iterables of different lengths, consider using itertools.zip_longest() from the standard library. You can also use zip() to transpose data structures: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] transposed = list(zip(*matrix)) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)] 3. any() and all() – Boolean Logic Made Simple These two functions are incredibly useful for checking conditions across collections. They can replace complex loops and make your Boolean logic much more readable. any() returns True if at least one element in the iterable is truthy: # Check if any number is even numbers = [1, 3, 5, 8, 9] has_even = any(n % 2 == 0 for n in numbers) # True # Check if any string starts with ‘A’ words = [‘banana’, ‘Apple’, ‘cherry’] starts_with_a = any(word.startswith(‘A’) for word in words) # True all() returns True only if all elements in the iterable are truthy: # Check if all numbers are positive numbers = [1, 5, 8, 12] all_positive = all(n > 0 for n in numbers) # True # Validate all required fields are filled form_data = {‘name’: ‘John’, ’email’: ‘john@email.com’, ‘age’: 25} required_fields = [‘name’, ’email’, ‘age’] all_filled = all(form_data.get(field) for field in required_fields) # True These functions short-circuit, meaning they stop evaluating as soon as the result is determined, making them efficient for large datasets. 4. getattr() – Dynamic Attribute Access with Grace The getattr() A function allows you to access object attributes dynamically and provides a safe way to handle missing attributes. This is particularly useful when working with APIs, configuration objects, or when you need to access attributes based on user input. Basic usage: class Config: debug = True database_url = “postgresql://localhost/mydb” config = Config() # Dynamic attribute access attr_name = ‘debug’ value = getattr(config, attr_name) # True # With default value for missing attributes timeout = getattr(config, ‘timeout’, 30) # Returns 30 if ‘timeout’ doesn’t exist Real-world example: def format_user_data(user, fields): “””Format user data based on requested fields””” result = {} for field in fields: value = getattr(user, field, ‘N/A’) result[field] = value return result # Usage class User: def __init__(self, name, email): self.name = name self.email = email user = User(“Tarun”, “tarun@email.com”) formatted = format_user_data(user, [‘name’, ’email’, ‘phone’]) # {‘name’: ‘Tarun’, ’email’: ‘tarun@email.com’, ‘phone’: ‘N/A’} This is much safer than using direct attribute access or hasattr() checks, and it’s more concise than try-except blocks. 5. sorted() with Custom Key Functions – Sorting Like a Pro While most developers know about the sorted() function, many underutilize its key parameter, which unlocks powerful sorting capabilities. The key function determines what value to use for comparison when sorting. Sort by string length: words = [‘Python’, ‘Java’, ‘C’, ‘JavaScript’, ‘Go’] by_length = sorted(words, key=len) # [‘C’, ‘Go’, ‘Java’, ‘Python’, ‘JavaScript’] Sort complex objects: students = [ {‘name’: ‘Alice’, ‘grade’: 85, ‘age’: 20}, {‘name’: ‘Bob’, ‘grade’: 92, ‘age’: 19}, {‘name’: ‘Charlie’, ‘grade’: 78, ‘age’: 21} ] # Sort by grade (descending) by_grade = sorted(students, key=lambda x: x[‘grade’], reverse=True) # Sort by multiple criteria using tuples by_age_then_grade = sorted(students, key=lambda x: (x[‘age’], -x[‘grade’])) Advanced sorting with operator module: from operator import attrgetter, itemgetter # For objects with attributes class Person: def __init__(self, name, age): self.name = name self.age = age people = [Person(‘Alice’, 30), Person(‘Bob’, 25), Person(‘Charlie’, 35)] sorted_people = sorted(people, key=attrgetter(‘age’)) # For dictionaries data = [{‘name’: ‘Alice’, ‘score’: 95}, {‘name’: ‘Bob’, ‘score’: 87}] sorted_data = sorted(data, key=itemgetter(‘score’), reverse=True) The key parameter makes sorting incredibly flexible and readable, eliminating the need for complex comparison functions. Putting It All Together These built-in functions work beautifully together. Here’s an example that combines several of them: def analyze_student_performance(students, subjects): “””Analyze student performance across multiple subjects””” results = [] for student in students: # Use getattr to safely access grades grades = [getattr(student, subject, 0) for subject in subjects] # Use zip to pair subjects with grades subject_grades = list(zip(subjects, grades)) # Use all() to check if student passed all subjects (grade >= 60) passed_all = all(grade >= 60 for grade in grades) # Use any() to check if student has any excellent grades (>= 90) has_excellent = any(grade >=
5 Python Built-in Function That Will Make You a Better Coder Read More »

