Sam
Sam

Reputation: 39

Problem with removing elements from list if the element exists in another list

I have a list of lists which goes something like this...

data = [['09/10/2018', '11/10/2018', 'Subject A', 'Teacher Name A', 'Assignment text...'], ['09/10/2018', '10/10/2018', 'Subject B', 'Teacher Name B', 'Assignment text...']]

and I want to remove every element from that list in which the first three elements are in a .csv file.

Example CSV:

 09/10/2018, 11/10/2018, Subject A
 09/10/2018, 10/10/2018, Subject B
 24/09/2018, 01/10/2018, Subject E

This .csv may contain a row that does not match any of the lists in the data list.

My problem is that I am unable to successfully remove lists from the data list based on the .csv

Here is the relevant code:

data = pull_data() # Gets the list of lists
with open('Log.csv') as f: #Opens Log.csv
    prevData=[tuple(line) for line in csv.reader(f)] #Gets the data from the .csv
for sublist in data:
    for prevSub in prevdata:
        if(len(sublist) > 0 and len(prevSub) > 0):
            if sublist[0] == prevSub[0]:
                if sublist[1] == prevSub[1]:
                    if sublist[2] == prevSub[2]
                        data.pop(data.index(sublist)) # Should remove list from list of lists

Upvotes: 1

Views: 43

Answers (1)

jpp
jpp

Reputation: 164773

Here's one way. The idea is to construct a set of tuples from the csv file. Then use a list comprehension to filter data. This ensure O(1) lookup complexity. Tuple conversion is necessary since tuple is hashable, while list is not.

from io import StringIO
import csv

data = [['09/10/2018', '11/10/2018', 'Subject A', 'Teacher Name A', 'Assignment text...'],
        ['09/10/2018', '10/10/2018', 'Subject B', 'Teacher Name B', 'Assignment text...']]

x = StringIO(""" 09/10/2018, 11/10/2018, Subject A
 09/10/2018, 10/10/2018, Subject B
 24/09/2018, 01/10/2018, Subject E""")

# replace x with open('Log.csv')
with x as fin:
    set_of_tuples = set(map(tuple, csv.reader(fin, skipinitialspace=True)))

# apply filter
res = [i for i in data if tuple(i[:3]) not in set_of_tuples]

Upvotes: 1

Related Questions