Python useful Libraries
Python has several built-in functions and constructs that can be replaced or enhanced by external libraries for debugging, logging, or improving readability and flexibility.
print() -> icecream.ic()
the print() function can be replaced by icecream.ic() for better debugging. ic() adds extra information like the variable name and its value, along with the line of code where it's called, making it easier to trace code execution.
from icecream import ic
def func():
x = 10
y = 20
ic(x, y) # This will print the variable names and values
result = x + y
ic(result) # It also prints the result of the operation
func()open() -> pathlib.Path().open()
Python’s built-in open() function can be replaced by pathlib.Path().open() for better handling of file paths and file operations, as pathlib() provides an object-oriented approach to working with file paths.
from pathlib import Path
# Instead of open('file.txt', 'r')
path = Path('file.txt')
with path.open('r') as file:
content = file.read()range() -> itertools.count()
If you’re working with infinite loops or need a sequence generator, you can replace range() with itertools.count() for an endless sequence of numbers.
import itertools
# Instead of range(10)
for num in itertools.count(start=0, step=1):
if num == 10:
break
print(num)Get Ankita’s stories in your inbox
Join Medium for free to get updates from this writer.
map(), filter(), reduce() -> List comprehensions or generator expressions
# Instead of map(lambda x: x * 2, list)
doubled = [x * 2 for x in list]
# Instead of filter(lambda x: x > 0, list)
positive_numbers = [x for x in list if x > 0]
from functools import reduce
# Instead of reduce(lambda x, y: x + y, list)
sum_of_numbers = sum(list)try…except -> contextlib.suppress()
If you’re suppressing specific exceptions in a try…except block, you can use contextlib.suppress() to suppress the exceptions in a more concise way.
from contextlib import suppress
# Instead of try-except:
# try:
# foo()
# except SomeException:
# pass
with suppress(SomeException):
foo()sorted() → heapq.nsmallest() or heapqnlargest()
If you only need the smallest or largest items in a list, instead of sorting the entire list, you can use heapq.nsmallest() or heapqnlargest() for better performance.
import heapq
# Instead of sorted(list) to get the smallest 3 elements
smallest_three = heapq.nsmallest(3, list)collections.Counter() → pandas.value_counts()
If you’re using collections.Counter() to count elements in a list, and you're working with data that can be represented as a table (e.g., a DataFrame), you can use pandas.value_counts() for a more powerful and efficient solution.
import pandas as pd
# Instead of collections.Counter
counts = pd.Series([1, 2, 2, 3, 3, 3]).value_counts()Python has many functions that can be replaced or enhanced with third-party libraries, often providing better functionality, easier usage, and additional features. Choosing the right one depends on your specific needs, whether it’s debugging, logging, file handling, or working with data.






