15 Concise Python Tips for More Efficient Coding
This article presents fifteen practical Python techniques—including multi‑key sorting, data classes, list comprehensions, memory inspection, frequency analysis, dictionary merging, multiple return values, filter/map/zip usage, list reversal, element existence checks, flattening nested lists, and uniqueness verification—to help developers write cleaner, faster, and more readable code.
This guide introduces fifteen compact Python tricks aimed at improving code simplicity and performance.
Multi‑key sorting: By leveraging the stability of list.sort() and operator.itemgetter , you can sort a list of dictionaries first by age and then by name, ensuring that entries with the same name remain grouped by age.
Data classes: Introduced in Python 3.7, @dataclass reduces boilerplate, provides automatic __eq__ , type hints, and a helpful __repr__ , making it ideal for lightweight data containers.
List comprehensions: Replace explicit loops with concise expressions for generating lists, performing arithmetic, calling functions, or filtering elements (e.g., keeping only even numbers).
Memory usage inspection: Use sys.getsizeof() to examine the memory footprint of objects; note that range objects are memory‑efficient compared to materialized lists.
Finding the most frequent value: Combine max() with list.count or use a set to identify the element that appears most often in a list or string.
Merging dictionaries: For Python 3.5+ use unpacking ( {**d1, **d2} ) and for Python 3.9+ the | operator to combine dictionaries, with later keys overriding earlier ones.
Returning multiple values: Functions can return tuples of values; for more than three results, consider wrapping them in a data class for clarity.
Filtering with filter() : Provide a predicate function and an iterable; the result can be converted to a list, or achieve the same effect with a list comprehension.
Mapping with map() : Apply a function to each element of an iterable, such as squaring numbers, either via map() or a list comprehension.
Combining lists with zip() : Pair elements from multiple lists positionally to create tuples of corresponding items.
Reversing a list: Use slice notation my_list[::-1] for a quick in‑memory reversal.
Checking element existence: The in operator determines whether a value is present in a list, useful for membership tests.
Flattening nested lists: A double list comprehension can flatten two‑level nested lists; for deeper nesting, third‑party libraries like dm‑tree can handle arbitrary depth.
Uniqueness verification: Convert a list to a set and compare lengths to confirm all elements are unique.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.