Forums

set all items from firstdict to newdict unless a certain key

Hi all, I need to set all items in a dict to a new dict if the key != '_id'. I thought this would work (the dicts are in a list btw)

newl=[]
for index, x in enumerate(listofdict):
    newl[index]=dict((key, value) for key, value in x.items() if key != '_id')

but I get a list index out of range. Why is that? If I print(index) it's correct.

nevermind I did this now:

newl = []
for index, x in enumerate(mylist):
    newl.append(dict((key,value) for key, value in x.items() if key != '_id'))

and all seems well (I was getting rid of mongodb _id from my list of dicts)