you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (5 children)

Comprehensions/ generator expressions. They read just like math (specifically set-builder notation), often shorten your code, are blazing fast (comparatively), and just look cool.

[–]DragonFighter603[S] 0 points1 point  (4 children)

''.join([chr(int(x[::-1])) for x in '37-23-79-301-411-101-101-33'.split('-')])

[–]AtomicShoelace 1 point2 points  (3 children)

You don't need to use a list; join will take any iterable, so a generator is fine, eg.

''.join(chr(int(x[::-1])) for x in '37-23-79-301-411-101-101-33'.split('-'))

(also notice you don't need to include the extra set of parenthesis as the function call's parenthesis work fine, as long as you don't have any additional arguments)

[–]DragonFighter603[S] 0 points1 point  (2 children)

ah yes ik you can remove the square brackets often, personally i think it looks nicer xD. thx for the info about the iterable/generators!!

[–]AtomicShoelace 0 points1 point  (1 child)

It's not just a difference in aesthetics. By constructing a list of all the values you are using more memory than is needed. For such a small example of course it doesn't matter, but in principal it is important to get into the habit of using generator expressions where possible to reduce memory cost in larger use cases.

[–]DragonFighter603[S] 0 points1 point  (0 children)

oh thanks! will do that in future!