0

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?

adamcasey
  • 9
  • 6

5 Answers5

2

You can just cast it to a set. Like this:

listTwo = set(listOne)
print(listTwo)

This will only keep the unique elements in listOne.

Adeel Ahmad
  • 1,033
  • 1
  • 11
  • 24
2

As it has already been answered you can use python set.

However, nobody asked about whether you need to keep the order of the original list since set does not keep the order of the original list. If you need to keep the order of the original list you can use OrderedDict:

from collections import OrderedDict

listOne = ['strOne', 'strTwo', 'strThree', 'strOne', 'strTwo', 'strOne']
listTwo = list(OrderedDict.fromkeys(listOne))
print(listTwo)
Nurjan
  • 5,889
  • 5
  • 34
  • 54
1

Here you go:

listTwo = [item.strip() for item in set(listOne)]
Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
1

The simplest thing is use SET , it will remove all duplicate strings. Further more you can convert it into list

Kumar
  • 131
  • 1
  • 10
1

Use set to remove duplicates in the list.

listOne = list(set(listOne))

If the values in list has spaces, then you can strip each item in the list and make it a set

listOne = list(set([x.strip() for x in listOne]))

Remember, both the above answers will not retain the order of elements.

voidpro
  • 1,652
  • 13
  • 27