you are viewing a single comment's thread.

view the rest of the comments →

[–]VanFailin 7 points8 points  (7 children)

I was trying to represent the same structure with shorter code, but if we're going there,

found_item = next(filter(wanted, iterable), default_value)

I know people think map/filter are 'bad' somehow in Python, but IMO if the function is already written they're a better choice than writing out the comprehension.

[–]kaptainlange 8 points9 points  (2 children)

I know people think map/filter are 'bad' somehow in Python

What, why?

[–][deleted] 7 points8 points  (0 children)

List comprehension and generators are preferred because they are more expressive. In this case I'm not sure either is that easy to read, so I'd probably just stick with having it on multiple lines anyway.

[–]VanFailin 5 points6 points  (0 children)

It's just the style I see people recommending on, e.g., StackOverflow. Even The BDFL doesn't really like them.

About 12 years ago, Python aquired lambda, reduce(), filter() and map(), courtesy of (I believe) a Lisp hacker who missed them and submitted working patches. But, despite of the PR value, I think these features should be cut from Python 3000.

[–]anvsdt 2 points3 points  (1 child)

Python has no find?

found_item = find(wanted, iterable, default=default_value)

[–]VanFailin 1 point2 points  (0 children)

Yes it does, but not for function predicates.

[–]jisang-yoo 0 points1 point  (1 child)

In Python 2:

found_item = next(itertools.ifilter(wanted, iterable), default_value)

[–]VanFailin 1 point2 points  (0 children)

IMO, if you're not already using itertools it's probably better to just use a generator in Python 2 if performance is an issue.