On Jun 7, 1:24 pm, kj <[EMAIL PROTECTED]> wrote: > The original Perl function takes a reference to an array, removes > from this array all the elements that satisfy a particular criterion, > and returns the list consisting of the removed elements. Hence > this function returns a value *and* has a major side effect, namely > the target array of the original argument will be modified (this > is the part I suspect may be un-Pythonic). > > Can a Python function achieve the same effect? If not, how would > one code a similar functionality in Python? Basically the problem > is to split one list into two according to some criterion. >
If you want to avoid side-effects completely, return two lists, the list of matches and the list of non-matches. In this example, partition creates two lists, and stores all matches in the first list, and mismatches in the second. (partition assigns to the associated element of retlists based on False evaluating to 0 and True evaluating to 1.) def partition(lst, ifcond): retlists = ([],[]) for i in lst: retlists[ifcond(i)].append(i) return retlists[True],retlists[False] hasLeadingVowel = lambda x: x[0].upper() in "AEIOU" matched,unmatched = partition("The quick brown fox jumps over the lazy indolent dog".split(), hasLeadingVowel) print matched print unmatched prints: ['over', 'indolent'] ['The', 'quick', 'brown', 'fox', 'jumps', 'the', 'lazy', 'dog'] -- Paul -- http://mail.python.org/mailman/listinfo/python-list