I started learning python within the last month. I recently came across a code example where a counter in the code was incremented based on a condition. The way the author did it was:
x = 0
x += [-1, 1][a == b]
From testing this works the same as if you used an if a==b: increment, else: decrement
.
I can't find this syntax anywhere else I've looked in the python documentation. It seems quite powerful and allows a variety of conditional assignments and aids conciseness.
Is there a reason I shouldn't use this structure also what is the structure doing?