I want to remove duplicates in the list, but append elements in the list when there are no duplicates in the existing data but only in the extend()
method, not in the append()
method (because I will treat an added list in the append()
method as one distinctive datum).
from collections import UserList
class Ulist(UserList):
def __init__(self, list_=[]):
UserList.__init__(self)
self.list_=list_
def __add__(self, another):
for i in another:
if i in self:
raise ValueError
print("Duplicated Data!")
else:
return UserList.__add__(self, another)
def append(self, another):
if another in self:
raise ValueError
print("Duplicated Data!")
else:
return UserList.append(self, another)
def extend(self, another):
for x in another:
if x in self:
print("Duplicated Data! Erase the Duplicated Data")
listList=UserList.extend(self, another)
listList=list(set(listList))
return listList
else:
return UserList.extend(self, another)
And this is the error message when I work with the list having an element duplicated with the existing list. I don't understand how TypeError: 'NoneType' object is not iterable
applies. How do I correct the extend()
method?
>>> l=Ulist()
>>> l.__add__([98])
[]
>>> l.append('kim')
>>> l.extend(['lee', 'park', 'choi'])
>>> l.append(['kim', 'jeong', 'kang'])
>>> l.extend(['lee', 'kim', 'joo'])
Duplicated Data! Erase the Duplicated Data
Traceback (most recent call last):
File "<pyshell#110>", line 1, in <module>
l.extend(['lee', 'kim', 'joo'])
File "C:\Users\소현\AppData\Local\Programs\Python\Python38-32\User_list.py", line 28, in extend
listList=list(set(listList))
TypeError: 'NoneType' object is not iterable