When writing automation scripts or quick data-processing utilities in Python, keeping code clean and readable makes a huge difference. Here are three simple Python tricks I use almost daily to avoid writing unnecessary boilerplate loops.

1. Merging Two Dictionaries Cleanly (Python 3.9+)

Instead of using .update() or verbose loops, you can merge dictionaries in a single expression using the | operator:

default_headers = {"Accept": "text/html", "DNT": "1"} custom_headers = {"User-Agent": "Mozilla/5.0", "DNT": "0"} final_headers = default_headers | custom_headers print(final_headers) # Output: {'Accept': 'text/html', 'DNT': '0', 'User-Agent': 'Mozilla/5.0'} 

2. Removing Duplicates While Preserving Order

Converting a list to a set() removes duplicates, but it scrambles the original order. Using dict.fromkeys() keeps everything in exact sequence:

urls = [ "[https://example.com/page1](https://example.com/page1)", "[https://example.com/page2](https://example.com/page2)", "[https://example.com/page1](https://example.com/page1)", "[https://example.com/page3](https://example.com/page3)" ] unique_urls = list(dict.fromkeys(urls)) print(unique_urls) 

3. Safe Dictionary Lookups with .get()

To prevent KeyError crashes when parsing messy JSON or API responses, always chain .get() with a fallback default:

api_response = {"status": 200, "data": {"items": 42}} count = api_response.get("data", {}).get("items", 0) print(count) # 42 

What are some of your favorite Python shortcuts for everyday scripting? Drop them in the comments!