Python 从列表中删除重复项,而不会使列表索引超出范围

Python 从列表中删除重复项,而不会使列表索引超出范围,python,Python,我正在尝试删除所有到期日等于当前日期的项目,但是,当我使用此代码时,因为我正在从列表中删除项目,它会更改长度,然后错误列表索引超出范围。我该如何解决这个问题 for i in range(len(self.__items)): if(self.__items[i]['duedate']==self.__currentDay): self.__items.remove(self.__items[i]) self.__numItems-=1 在迭代期

我正在尝试删除所有到期日等于当前日期的项目,但是,当我使用此代码时,因为我正在从列表中删除项目,它会更改长度,然后错误列表索引超出范围。我该如何解决这个问题

 for i in range(len(self.__items)):
     if(self.__items[i]['duedate']==self.__currentDay):
         self.__items.remove(self.__items[i])
         self.__numItems-=1

在迭代期间更改列表长度时,必须反向迭代:

for i in range(len(self.__items)-1, -1, -1):
    if (self.__items[i]['duedate'] == self.__currentDay):
        self.__items.remove(self.__items[i])
        self.__numItems -= 1

self.\uu items=[i代表self中的i.\uu items如果i['duedate']!=self.\uu currentDay]
。摆脱self.\uu numItems,只使用
len(self.\uu项目)
。这能回答你的问题吗?