r/Python • • Feb 28 '13

What's the one code snippet/python trick/etc did you wish you knew when you learned python?

I think this is cool:

import this

259 Upvotes

307 comments sorted by

View all comments

Show parent comments

6

u/edeloso Feb 28 '13

I still have a fondness for the dictionary based example.

Though, to match the ternary at the top of this subthread it would be:

a = {True: b, False:d }[c]

I like it because I feel it's the most explicit of the choices.

4

u/thatdontmakenosense Feb 28 '13

A list/tuple would be cleaner IMO, although the two expressions get reversed:

a = [d, b][c]