elegantly splitting a list (or dict) into two via some arbitrary function in python

Viewed 276

Is there any elegant way of splitting a list/dict into two lists/dicts in python, taking in some arbitrary splitter function?

I could easily have two list comprehensions, or two selects, but it seems to me there should be some better way of doing it that avoids iterating over every element twice.

I could do it easily with a for loop and if statement, but that takes something like 7 lines of code for what should be a very simple operation.

Any ideas?

Edit:

Just for reference, my two solutions would be,

# given dict cows, mapping cow names to weight
# fast solution
fatcows = {}
thincows = {}
for name, weight in cows:
    if weight < 100:
        thincows[name] = weight
    else:
        fatcows[name] = weight

# double-list-comprehension solution would be
fatcows = {name: weight for name, weight in cows.items() if weight > 100}
thincows = {name: weight for name, weight in cows.items() if weight < 100}

I was thinking there must be something more elegant than this that i never thought of, something like:

thincows, fatcows = ??? short expression involving cows ???

I know it's possible to do by writing higher order functions stuff to do it for me, and i know how to do it manually. I was just wondering if there was some super-elegant language feature to do it for me.

It's like you can write your own subroutines and whatnot to do a SELECT on a list, or you can just say

thincows = select(cows, lambda c: c.weight < 100)

I was hoping there would be some similarly elegant way of splitting the list, with one pass

6 Answers
Related