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 Alan Isaac for How can I partition (split up, divide) a list based on a condition?

$
0
0

I basically like Anders' approach as it is very general. Here's a version that puts the categorizer first (to match filter syntax) and uses a defaultdict (assumed imported).

def categorize(func, seq):"""Return mapping from categories to lists    of categorized items."""    d = defaultdict(list)    for item in seq:        d[func(item)].append(item)    return d

Viewing all articles
Browse latest Browse all 41

Trending Articles



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