0

I am new to coding and was trying solve a question on here posed previously. Have a list (lst) of dictionaries. Want to create a new list (newlist) of dictionaries, with the count of identical dictionaries in the original list, in new list as keyed item in each dictionary. My code below adds the count of dictionaries to dictionary of original list and I can't see why.

lst = [{'time': 10, 'stuff': 'abcd'},{'time': 10, 'stuff':'abcd'},{'time': 11, 'stuff': 'bcd'},{'time': 12, 'stuff': 'dc'},{'time': 13, 'stuff': 'ak'},{'time': 11, 'stuff': 'bcd'}]
newlist= []
m = 0
while len(lst)>0 :
    y = lst[0]
    ycount = lst.count(y)
    newlist=newlist + [y]
    newlist[m]['count'] = ycount
    print(f'lst = {lst}\n')
    while y in lst:
        lst.remove(y)
    m += 1
print(f'newlist= {newlist}')
MarkJPD
  • 1
  • 1

0 Answers0