Quantcast
Channel: How can I partition (split up, divide) a list based on a condition? - Stack Overflow
Viewing all articles
Browse latest Browse all 41

Answer by jcb for How can I partition (split up, divide) a list based on a condition?

$
0
0

If your concern is not to use two lines of code for an operation whose semantics only need once you just wrap some of the approaches above (even your own) in a single function:

def part_with_predicate(l, pred):    return [i for i in l if pred(i)], [i for i in l if not pred(i)]

It is not a lazy-eval approach and it does iterate twice through the list, but it allows you to partition the list in one line of code.


Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>