Re: List Partition Comprehension (Feature Suggestion)

2020-09-22 Thread Terry Reedy
On 9/22/2020 3:16 PM, Yakov Shalunov wrote: Python list comprehension is substantially faster than plan iteration, to the point where ``` l0, l1 = [],[] for x in l: if cond(x): l0.append(x) else: l1.append(x) ``` runs at about the same speed as ``` l0 = [x for x in l

Re: List Partition Comprehension (Feature Suggestion)

2020-09-22 Thread Greg Ewing
On 23/09/20 7:16 am, Yakov Shalunov wrote: l0, l1 = ([x for x in l if cond(x) else x]) l0, l1, l2 = ([x for x in l if cond0(x) else x**2 if cond1(x) else x**3]) This syntax seems a bit convoluted. One of the result expressions is at the beginning as usual, but the rest are tacked on the end. An

Re: List Partition Comprehension (Feature Suggestion)

2020-09-22 Thread Yakov Shalunov
A possible alternative would be a partition library function in the same vein as `map` and `filter` ``` def partition(n, key, iter): """ Partitions a list. Args: (n: the number of partitions), (key: function which takes elements and returns the index of the partition to place them in)