My code is all loops and dicts

I have been coding Python for over 10 years now, but recently I started to reflect on how my code actually looks.

Whatever pattern I am applying my code end up (most of the time) consisting of loops and dicts.

Must of my objectives is manipulating objects based on conditions. Basically all my code end up looking like this pseudo code:

d = {}

for key, value in dict.items() if X: Y = func(value) for x in Y: if x: d[key] = x else: d[key] = x + 2

(With new lines ofc)

It’s hard to follow, it’s boring and iterations becomes iterations in iterations.

Any suggestions? Feedback?

Thanks my dudes.

That kind of code is normal, especially for python. It’s a procedural language with some very powerful built in objects. lists, dicts, and for loops dominate a lot of python code. If you want more readability, you can get pretty far with good naming, guard clauses, comprehensions, functions, and zip/itertools/more-itertools. Studying other people’s code is the best way to improve your own, but I remember getting a lot out of a book called Effective Python FWIW. You can also clean/refactor your data, not just at the source but throughout your program to simplify any code that has to touch it. Ugly data is a leading cause of ugly code.

If you want to get away from for loops and stateful code, try a language with better support for function composition. Elixir is nice and Clojure has access to everything from Java.