Modern Python dicts remember insertion order: This applies in for loops, too!
Using Python defaultdict? Don’t pass a value: Pass a function that returns the default:
Your Python function takes a filename and **kwargs. But this raises an error, since “filename” is passed twice: Solution: Set filename to be positional only: Now any keyword argument works!
Break a Python string into a list of strings with str.split: Limit splits with maxsplit: Or split from the right, with a limit:
Tired of super-long Python Pandas backtraces in Jupyter or IPython? Use the %xmode magic method: %xmode Minimal Now you just see the error, not 50 lines of library internals. You can also set it to Plain, Context, or Verbose.
Joining Python Pandas DataFrames with overlapping columns? You’ll hit: ValueError: columns overlap but no suffix specified Fix with lsuffix and rsuffix: Now the columns are: x_left, x_right, y_left, y_right Problem solved!
Think you can’t modify a Python list while iterating? You can append to it: This prints 10, 20, 30, 100, 400, and 900. BUT don’t insert or remove. That’ll cause real trouble.
Think Python functions can return multiple values? They actually return ONE value, a tuple: Unpack it into separate variables:
Let users assign using [] in Python, with custom logic with __setitem__: sl.d[‘2’, ‘4’, ’99’, ‘8’]
The __getitem__ method in Python can do more than simple lookups: Use [] for custom logic — returning filenames, random numbers, or database records.