After reading some data from a file and sorting through it, I get this.
[['John', 1], ['Lisa', 2], ['Carly', 2], ['Zacharry', 1], ['Brian', 3], ['John', 5], ['Carly', 2]]
How can I removed the duplicates while also adding the values they have so my output would look like this
[['John', 6], ['Lisa', 2], ['Carly', 4], ['Zacharry', 1], ['Brian', 3]]
I've been able to isolate the duplicates on their own with the total sum of data, however I have no idea how to get my desired output.
Note: Order of the list is important in my case and that my data stays in a list
When I've isolated the duplicates I get this output:
[['John', 6], ['Carly', 4]]
My Code:
def create_bills(filename, capacity):
fob = open(filename)
newlst = list()
for line in fob:
a = line.split(" $")
b = [a[0], int(a[1])]
newlst.append(b)
print(newlst)
newlst2 = list()
for i in range(len(newlst)):
n = i + 1
while n < len(newlst):
if newlst[i][0] == newlst[n][0]:
newlst2.append([newlst[i][0], (newlst[i][1] + newlst[n][1])])
n += 1
newlst3 = list()
for i in range(len(newlst)):
pass
print(newlst2)
Thank you!