5 Python One-Liners Every ML Engineer Should Know

When you’ve been in Machine Learning long enough, you realize that a lot of your job isn’t just building fancy neural networks, it’s data wrangling, feature engineering, debugging, and quick experiments. And in those moments, small Python cheat codes save you hours. So, in this article, I’ll show you some of my favourite Python one-liners that every ML Engineer should know.

5 Python One-Liners Every ML Engineer Should Know

Below are my 5 favourite Python one-liners that make real ML work faster, cleaner, and less error-prone.

Quickly Flatten a List of Lists

flat_list = [item for sublist in nested_list for item in sublist]

It turns something like [[1, 2], [3, 4], [5]] into [1, 2, 3, 4, 5].

As an ML Engineer, you’ll often end up with nested lists, think tokenized text data, batches of predictions, or grouped categorical values. Instead of writing nested loops, one clean line flattens everything.

Here’s an example:

nested_list = [[1, 2], [3, 4, 5], [6], [7, 8, 9]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Count Elements in a List (Like a Pro)

from collections import Counter
counts = Counter(my_list)

It gives you a dictionary-like object showing the frequency of each element in my_list.

It’s useful when you’re exploring data, knowing category distribution is critical, for class imbalance checks or quick sanity tests before model training.

Here’s an example:

from collections import Counter

my_list = ['spam', 'ham', 'spam', 'egg', 'ham', 'spam', 'egg']

# count occurrences
counts = Counter(my_list)

print(counts)
print(counts['spam'])      # how many times 'spam' appears
print(counts.most_common()) # items sorted by frequency
Counter({'spam': 3, 'ham': 2, 'egg': 2})
3
[('spam', 3), ('ham', 2), ('egg', 2)]

Filter a Pandas DataFrame in One Line

filtered_df = df[df['column_name'] > 100]

It returns a DataFrame with only rows where the column value is greater than 100 (or any other value).

It’s useful when data cleaning and preprocessing often involve quick filtering, such as removing outliers, selecting only recent records, or keeping high-value customers for churn prediction.

For example, you can use it in a scenario to keep only transactions above ₹1,000 when building a fraud detection model.

Remove Duplicates While Preserving Order

unique_items = list(dict.fromkeys(my_list))

It takes a list, removes duplicates, and keeps the original order of elements.

It’s helpful for preprocessing categorical data or feature lists. You often need unique values without shuffling the order, for example, when generating label encodings or cleaning up tokenized text.

Here’s an example:

my_list = ['cat', 'dog', 'cat', 'rabbit', 'dog']
unique_items = list(dict.fromkeys(my_list))
print(unique_items)
['cat', 'dog', 'rabbit']

Merge Multiple CSV Files Into One DataFrame

import pandas as pd, glob
df = pd.concat([pd.read_csv(f) for f in glob.glob("data/*.csv")], ignore_index=True)

It reads all CSV files in the data/ folder and combines them into one DataFrame.

Datasets are often split across multiple files, such as monthly logs, chunked training data, or scraped batches. This line merges them instantly.

For example, you can use it while merging daily sales logs into one dataset before running time series forecasting.

Final Words

These one-liners aren’t Python tricks for the sake of being clever. They’re shortcuts that make real ML work faster, cleaner, and less error-prone. If you’re starting, try adding these to your daily workflow; they’ll quietly save you more time than you expect. I hope you liked this article on 5 Python One-Liners Every ML Engineer Should Know. Feel free to ask valuable questions in the comments section below. You can follow me on Instagram for many more resources.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2099

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading