Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to "fork" a list in two based on a condition [duplicate]

Tags:

python

list

I have seen a pattern repeated a couple times in my team's code, it looks like this

numbers = [1, 2, 3, 4]
even_numbers = [n for n in numbers if n % 2 == 0]
odd_numbers = [n for n in numbers if n % 2 != 0]

I was wondering if there is a function somewhere (I have looked around but haven't been able to find it) that would do something like this

numbers = [1, 2, 3, 4]
even_numbers, odd_numbers = fork(numbers, lambda x: x % 2 == 0)

So, this function I am looking for, would receive an iterable and a function, and return two lists, one would be the values that match a provided condition, and the other would be the ones that didn't.

Is there something around python's standard library that achieves this?

like image 381
Jose Enrique Narvaez Avatar asked Aug 08 '18 09:08

Jose Enrique Narvaez


People also ask

How do I make a two list identical in Python?

list() takes any iterable and produces a new list from it, so we can use this to copy list1 , thus creating two lists that are exactly the same.

How do you split a list by value in Python?

The naive way to split a list is using the for loop with help of range() function. The range function would read range(0, 10, 2) , meaning we would loop over the items 0,2,4,6,8 . We then index our list from i:i+chunk_size , meaning the first loop would be 0:2 , then 2:4 , and so on.


1 Answers

I usually call this sift, but partition is fine too.

Another, itertools-less implementation might be

def sift(iterable, predicate):
    t = []
    f = []
    for value in iterable:
        (t if predicate(value) else f).append(value)
    return (t, f)

even, odd = sift([1, 2, 3, 4, 5], lambda x: x % 2 == 0)

EDIT: for a slightly more complex implementation that is about 30% faster (on my Python installation anyway):

def sift2(iterable, predicate):
    t = []
    f = []
    ta = t.append
    fa = f.append
    for value in iterable:
        (ta if predicate(value) else fa)(value)
    return (t, f)
like image 102
AKX Avatar answered Oct 07 '22 23:10

AKX