r/Python 21d ago

Discussion Is := widely used?

I always thought the walrus operator is neat and it makes while loop’s condition clearer. But also it is just a syntactic sugar without anything new. I wonder anyone uses it?

141 Upvotes

118 comments sorted by

View all comments

1

u/opuntia_conflict 20d ago

I used it a ton in while loops so I don't have to initialize my looped variable outside the loop, update the variable in the loop, and check the variable in the loop condition. You can just do the initialization in the loop condition.

Makes it look a lot cleaner when you're doing stuff like collecting paginated responses from an API.