
10 Python One-Liners That Will Make You Look Like a Senior Developer
I've been reviewing code for years. The difference between junior and senior Python isn't frameworks — it's knowing the language's built-in power. Here are 10 one-liners I use daily that consistently impress in code reviews. 1. Flatten a Nested List # Junior flat = [] for sublist in nested : for item in sublist : flat . append ( item ) # Senior flat = [ item for sub in nested for item in sub ] # Even shorter (if you need it truly flat at any depth) import itertools flat = list ( itertools . chain . from_iterable ( nested )) 2. Count Occurrences # Junior counts = {} for item in data : counts [ item ] = counts . get ( item , 0 ) + 1 # Senior from collections import Counter counts = Counter ( data ) # Bonus: counts.most_common(3) gives top 3 3. Merge Two Dictionaries # Junior merged = dict1 . copy () merged . update ( dict2 ) # Senior (Python 3.9+) merged = dict1 | dict2 # Or (Python 3.5+) merged = { ** dict1 , ** dict2 } 4. Read a File into a List (Stripped) # Junior lines = [] with open
Continue reading on Dev.to Tutorial
Opens in a new tab




