Nirav Joshi
Nirav Joshi

Reputation: 79

How to find unique values in python list

I have following list coming from file. what i want one of list of this file which containts many values duplicate one also

['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['7', '3', '4', '\n']
['7', '3', '4', '\n']
['7', '3', '4', '\n']
['7', '3', '4', '\n']

How to filter out only unique values.

 import os
os.chdir('C:\\')
if os.path.exists('xxx.dat'):
    airport = open('routes.dat')
    for each_line in airport:
        (Airlinecode,AirlineID,Source_airport_code,Source_airport_ID,Destination_airport,Destination_airport_ID,Codeshare,Stops,Equipment) = each_line.split("\t")
        newlist =[]
        newlist = Equipment
        i = iter(newlist)
        elements = []

        for eachitem in range(len(newlist)):
            elements.append(i.next())
        print (elements)
    airport.close()

else:
    print('file does not exists')

Upvotes: 1

Views: 734

Answers (1)

aaron-prindle
aaron-prindle

Reputation: 3297

For something like this where you are generating list of lists as output, you cannot simply put the items in a set (which does not allow duplicates) as lists are unhashable. What you could do is when generating the list, do something like:

seen = set()
key = "".join(newlist)
if key not in seen:
  seen.add(key)
  output.append(newlist)

If for some reason you cannot do this during the first iteration through the text file and only have access to the output array values, then you could loop through your output doing a similar technique and simply keep the unique values.

Upvotes: 1

Related Questions