12 ms·
My Favorite Pieces of Syntax in 8 Different Programming Languages
- perfunctory 6y agodef even_numbers(limit): return [num for num in range(0, limit, 2)] It’s unfortunate they chose this specific example because it doesn’t really show the power of list comprehensions. This example could be written as return list(range(0, limit, 2)) Or even simply return range(0, limit, 2) If one doesn’t need a mutable list.
- yakkomajuri 6y agoGreat point! Was trying to keep examples simple but that just flew over my head. Updated the article. How about the new example? def squares(limit): return [num*num for num in range(0, limit)] If you have another suggestion I'd be happy to hear it!
- perfunctory 6y agoMuch better.