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

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

$
0
0

I have some code like:

good = [x for x in mylist if x in goodvals]bad = [x for x in mylist if x not in goodvals]

The goal is to split up the contents of mylist into two other lists, based on whether or not they meet a condition.

How can I do this more elegantly? Can I avoid doing two separate iterations over mylist? Can I improve performance by doing so?


Viewing all articles
Browse latest Browse all 41

Latest Images

Trending Articles





Latest Images