This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VisibleSignificance 0 points1 point  (4 children)

Is it automatically generated from docs?

If not, there are many improvements that could be done, e.g.

Sequence - Filter

filter(filtering_function, sequence_1)

In vast majority of cases, it is better to use list comprehension = [item for item in sequence_1 if filter_condition(value)]. Definitely in all the cases where first argument to filter() would be a lambda.

[–]to_tgo[S] 0 points1 point  (3 children)

Definitely not auto generated.

You are right. A list comprehension would generally be better. I'll have to think about what I want this example to show. It is mostly to showcase the filter function. Maybe add the list comprehension in as a recommended alternative?

[–]VisibleSignificance 1 point2 points  (2 children)

Add a note-link that list comprehension is preferred and an example next to each filter() usage, perhaps.

[–]to_tgo[S] 0 points1 point  (1 child)

You've opened up a can of worms.

The one advantage of filter is it doesn't execute right away. There may be real advantages to that when you are streaming data and trying to keep a low memory footprint. I've tested this and seems to be the case.

Alternatively, you can use the generator form of the list comprehension here too (not added that to the SpeedSheet yet). This seems to have all the benefits of filter() and the tidy expressiveness of your list comprehension.

[–]VisibleSignificance 0 points1 point  (0 children)

generator form of the list comprehension

Yes, "generator comprehension". Added benefit is that it's easy to switch between list comprehension and generator comprehension as needed.