I'm trying to merge counts for items (URLs) in the list:
[['foo',1], ['bar',3],['foo',4]]
I came up with a function, but it's slow when I run it with 50k entries. I'd appreciate if somebody could please review and suggest improvements.
def dedupe(data):
''' Finds duplicates in data and merges the counts '''
result = []
for row in data:
url, count = row
url_already_in_result = filter(lambda res_row: res_row[0] == url, result)
if url_already_in_result:
url_already_in_result[0][1] += count
else:
result.append(row)
return result
def test_dedupe():
data = [['foo',1], ['bar',3],['foo',4]]
assert dedupe(data) == [['foo',5], ['bar',3]]