New answers tagged


Martin's answer is excellent. The one thing missing is how to extend CGPoint to conform to Hashable using the native hasher functions: extension CGPoint: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(x) hasher.combine(y) } } After running his tests, it too did not produce any collisions: 160000 160000 The nice thing ...


Use collections.defaultdict to "bucketize" the items in your list. Then collect the buckets to get the desired result. from collections import defaultdict bucket = defaultdict(list) for record in lst: bucket[record['name']].append(record) lst_new = list(bucket.values())


Should have spaces around =, and += would shorten it a bit. And I'd be consistent with the variable name (not switch from d to v) and use better names for the lists (l looks too much like 1 and is just too short for something presumably living longer (unlike d, which I find ok in such small contexts)): lst2 = [d.copy() for d in lst] for d in lst2: d['a'] ...

Top 50 recent answers are included