I have a list that holds multiple, duplicate strings that come from a .csv file:
listOne = ['strOne', 'strTwo', 'strThree', 'strOne', 'strTwo', 'strOne']
and want to create a new list from it to hold only unique strings:
listTwo = ['strOne', 'strTwo', 'strThree']
I read the file and populate the original list like this:
def createOrigList(filename):
dataFile = open(filename,'r')
for line in dataFile:
origList.append(line)
def createListOne():
for item in origList:
tempList = item.split(',')
strOne = tempList[0].strip()
listOne.append(strOne)
I've tried to implement this earlier post and use the Python if (... not in ...)
conditional nested into a for
loop to populate listTwo
but when I try to print out listTwo
, nothing has been added.
def createListTwo():
for item in listOne:
item = item.strip()
if (item not in listTwo):
listTwo.append(item)
Am I not comparing exact strings when trying to create listTwo
?