Adam
Adam

Reputation: 483

My python code to check for element exists in a list is not working

I have a SQL query result of 695 records which I need to strip out the matches to my 52 items in _ids list.

_ids = [44226, 44303, ...]

records = [
    (44226, datetime.datetime(2019, 8, 27, 10, 0), datetime.datetime(2019, 
    8, 27, 21, 0), '0.50'), 
    (44227, datetime.datetime(2019, 8, 30, 18, 0), datetime.datetime(2019, 
    8, 30, 22, 30), '0'), ...
]

Using the Is there a short contains function for lists? and looping over my records is always resulting in True.

for i in records:
    if i[0] not in _ids:
        # pop from list

Any recommendations to efficiently return list only list of records that match _ids list?

Upvotes: 1

Views: 55

Answers (1)

Selcuk
Selcuk

Reputation: 59184

From what it looks from your comment, you are calling .pop() inside the loop. This will modify the list while iterating it at the same time, leading to unexpected results. Try this:

import datetime 

_ids = [44226, 44303]

records = [
    (44226, datetime.datetime(2019, 8, 27, 10, 0), datetime.datetime(2019, 
    8, 27, 21, 0), '0.50'), 
    (44227, datetime.datetime(2019, 8, 30, 18, 0), datetime.datetime(2019, 
    8, 30, 22, 30), '0'),
]

print([row[0] for row in records if row[0] in _ids])

This uses a conditional list comprehension to return only the ids that match.

Update: An edit request to this answer pointed out that it is a good idea to define _ids as a set so that you can have constant time complexity in your lookup, i.e.:

_ids = {44226, 44303}

Upvotes: 3

Related Questions