1

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

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I thought I had tried something similar but must have made a typo somewhere. Thank you for the extremely fast response.

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.