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?

3
  • 3
    You can use a python set: listTwo = list(set(listOne)) Commented Jun 15, 2017 at 2:55
  • have you defined list one and list two outside of the functions createListOne and CreateList two?if not,list add them before any function listOne=[],listTwo=[] Commented Jun 15, 2017 at 3:02
  • @Eliethesaiyan yes, the lists are defined outside the functions. Commented Jun 15, 2017 at 21:04

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

1

Here you go:

listTwo = [item.strip() for item in set(listOne)]

Comments

1

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.