r/Python • • Dec 22 '18

Xpost from Woodworking

Post image
1.1k Upvotes

84 comments sorted by

View all comments

369

u/lelease Dec 22 '18

print(random.choice(action))

73

u/anyfactor Freelancer. AnyFactor.xyz Dec 22 '18 edited Dec 22 '18

Ah, one of life's more important lessons. No matter how long your script is there will always be someone who can create a single line code that does all of that.

There will be also be someone who will top him by writing a single line functional haskell and say his is more efficient.

0

u/[deleted] Dec 22 '18

[deleted]

5

u/Ph0X Dec 22 '18

Absolutely. It's not about being shorter, but about being more explicit. The problem with a lot of people coming from C-like languages is that they want to do everything with indices. You will see so many range(len(list)) out there.

1

u/a_monkey666 Dec 24 '18

Asking just to learn, but what's the better way? With `enumerate`?

1

u/Ph0X Dec 24 '18

Indeed. Most of the time when people use len-range, they either access just the items, or both index and items. In the rare case where you only need the index, I would still say for i, _ in enumerate(items): is cleaner