Home » 7 Python Built-ins That Seem Like a Joke (Until You Use Them)

7 Python Built-ins That Seem Like a Joke (Until You Use Them)

7 Python Built-ins That Seem Like a Joke (Until You Use Them)
Image by Editor | ChatGPT

 

Introduction

 
Working with Python means relying on many of its built-in functions, especially for data science tasks. Popular functions like len, max, range, etc., are common in a data scientist’s toolkit and useful in various situations. However, many built-in functions remain unrecognized because they are perceived as useless.

In this article, we will explore seven different built-ins that you might think are a joke but are actually very useful in their applications. These built-ins are different from your usual code, but they will find their place in your workflow once you realize their usefulness.

Curious? Let’s get into it.

 

1. The divmod Built-in Function

 
Many people rarely use the divmod built-in function, or even know it exists. The divmod built-in function returns two numbers: the result of floor division and the modulus operation. It may seem useless since we can use syntax like a // b and a % b without the need for the built-in, especially when we rarely need both results at once.

In real-world use cases, we often need both results and want them computed once consistently to make the process faster. A few divmod applications — including time conversion, pagination, batching, and cleaner hash math — show its utility.

Let’s see a usage example:

total_seconds = 7132
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
print(f"Time: {hours:02d}:{minutes:02d}:{seconds:02d}")

 
Where the output is shown below:

 
With one function, we can split numbers evenly, which is useful in many applications.

 

2. The slice Built-in Function

 
The slice built-in function is used to separate or extract parts of sequences like strings, lists, or tuples. It may seem redundant because we can simply create a slice object like obj[1:10:2].

However, the strength of the slice built-in function becomes apparent when you need to reuse the same slicing rule across various objects. The function helps us maintain consistent parsing logic and allows us to build strategies for data processing.

Here is an example for the implementation:

evens = slice(0, None, 2)
text = "abcdefghij"
print(text[evens])

 
The output is shown below:

 
The slice built-in above shows that we can set a reusable rule to take every second character.

 

3. The iter Built-in Function

 
The iter built-in function creates an iterator object that processes items sequentially, one by one. This may seem unnecessary since we already have the `while True` and `break` pattern. Yet, it is helpful as it allows for cleaner code and a better structure in the data pipeline.

For example, we can use iter to control the streaming data processing:

import io
f = io.BytesIO(b"12345678")

for block in iter(lambda: f.read(3), b""):
    print(block)

 
Where the output is shown below:

 

4. The memoryview Built-in Function

 
The memoryview built-in function creates a memory view object from internal data without copying it. It may seem like an unnecessary function that has no place in the standard Python workflow.

However, the memoryview function becomes useful when handling large binary data because it allows users to access and modify it without creating a new object. For example, you can replace a portion of the data in the same memory without copying, as shown in the following code.

buf = bytearray(b"hello")
mv = memoryview(buf)
mv[0] = ord('H')
print(buf)
mv[:] = b"HELLO"
print(buf)

 
The output is shown below:

bytearray(b'Hello')
bytearray(b'HELLO')

 
The assignment is done in the same memory, which is helpful for any performance-sensitive work.

 

5. The any Built-in Function

 
The any built-in function returns a Boolean value by checking items in an iterable object. It returns True if any element in the iterable is truthy and False otherwise. The function seems useless, as it appears to replace a simple checking loop.

However, the strength of the any function lies in its ability to avoid creating temporary objects and to use lazy evaluation with generators, which means that the function can have clear loop logic and reduce memory usage.

An example of the Python code is shown below:

files = ["report.txt", "sales.csv", "notes.md"]
print(any(f.endswith(".csv") for f in files))

 
Where the output is shown below:

 
It’s beneficial when we have use cases that require validation or guard conditions.

 

6. The all Built-in Function

 
The all built-in function is similar to any, but the difference is that the former only returns True if all objects in the iteration are truthy. Similar to any, the all built-in function is often perceived as useless since a manual loop can achieve the same result.

It is a valuable built-in because it provides declarative and short-circuit verification that every element is truthy with lazy evaluation for generators. This means that the code is cleaner and takes less memory.

Example usage is using the following code:

required = ["id", "name", "email"]
record = {"id": 1, "name": "Ana", "email": "a@x.io"}
print(all(k in record for k in required))

 
Where the output is shown below:

 
Applications include schema checks and input validation.

 

7. The zip Built-in Function

 
The zip built-in function is used to aggregate elements from multiple iterable objects into tuples. It may seem unnecessary, as a developer could simply iterate with indices, such as with a for i in range(len(x)): loop, or because it only returns tuples.

The zip built-in function, however, is far more helpful because it allows you to loop over multiple iterable objects without juggling indices. As the function creates pairs only when necessary, it will not waste memory. It also avoids any common index mistakes when we manually manage the indexing.

The Python example is shown below:

keys = ["id", "name", "email"]
vals = [1, "Ana", "a@x.io"]
record = dict(zip(keys, vals))
print(record)

 
Where the output is shown below:

{'id': 1, 'name': 'Ana', 'email': 'a@x.io'}

 
Many applications exist, such as for parallel iteration over datasets or pairwise computations without index arithmetic.

 

Conclusion

 
Python is a useful programming language for any data-related activity, and its built-in functions are a significant part of that utility. However, some built-ins are perceived as useless — that is, until you actually use them and realize just how valuable they are.

I hope this has helped!
 
 

Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and data tips via social media and writing media. Cornellius writes on a variety of AI and machine learning topics.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *