DEV Community

Mostafa Gazar
Mostafa Gazar

Posted on

1 2

Some Python handy snippets for Machine Learning Engineers

Get file name and extension

from pathlib import Path

path = Path("dir/file.xml")

file_name = path.stem  # file
file_extension = path.suffix  # .xml
Enter fullscreen mode Exit fullscreen mode

Iterate through directory contents

from pathlib import Path

data_root = Path("/imgs")
for item in data_root.iterdir():
    print(item)
Enter fullscreen mode Exit fullscreen mode

You can also use glob

from pathlib import Path

data_root = Path("/imgs")

# Get all png images in this directory level
data_root.glob("*.png")
# Get all png images in this directory including sub-directories
data_root.glob("*.png", recursive=True)
Enter fullscreen mode Exit fullscreen mode

Inverse dictionary key/value => value/key

inversed_dict = {v: k for k, v in some_dict.items()}
Enter fullscreen mode Exit fullscreen mode

Flatten a list of lists

import operator

flat_list = reduce(operator.concat, some_list)
Enter fullscreen mode Exit fullscreen mode

Map string list to int list

elements = ["1", "2", "3"]

elements = list(map(int, elements))
Enter fullscreen mode Exit fullscreen mode

Count unique values in list and their total

from collections import Counter

elements = ["1", "2", "3", "2", "3", "3"]

# Unique values in list
keys = Counter(elements).keys()

# Frequency of their appearance
values = Counter(elements).values()
Enter fullscreen mode Exit fullscreen mode

Sort dict by value

for key in sorted(confidences, key=confidences.get, reverse=True):
    print(f'key {key}')
Enter fullscreen mode Exit fullscreen mode

Format f strings

print(f'2 decimal points {some_variable:.2f}')
Enter fullscreen mode Exit fullscreen mode

Batch an iterable

def batch(iterable, n=1):
    l = len(iterable)
    for index in range(0, l, n):
        yield iterable[index:min(index + n, l)]

# and call it like
for x, pred in enumerate(batch(preds, 32), 1):
    pass
Enter fullscreen mode Exit fullscreen mode

I am working on a project called ML Studio, want to get early access to and product updates? Subscribe here or follow me on twitter.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay