Steampunkery
Steampunkery

Reputation: 3874

Check conditions from two lists

I have 2 lists, excludeName and excludeZipCode, and a list of dictionaries search_results. I need to exclude some of the dictionaries from the list they are being copied in to (search_copy) based on several conditions. The index of the excluded name is the same as the index of the excluded zip code. Currently I am completely baffled, though I have tried many different ways to iterate over them and exclude. Another problem I've been having is having businesses added many times.

excludeName = ['Burger King', "McDonald's", 'KFC', 'Subway', 'Chic-fil-a', 'Wawa', 'Popeyes Chicken and Biscuits', 'Taco Bell', "Wendy's", "Arby's"]
excludeZip = ['12345', '54321', '45123', '39436', '67834', '89675', '01926', '28645', '27942', '27932']
while i < len(search_results):
        for business in search_results:
            for name in excludeName:
                occurrences = [h for h, g in enumerate(excludeName) if g == name]
                for index in occurrences:
                    if (business['name'] != excludeName[index]) and (business['location']['zip_code'] != excludeZip[index]):
                        search_copy.append(business)
        i += 1

Here's an example dictionary:

{
    'location': {
        'zip_code': '12345'
    },
    'name': 'Burger King'
}

Upvotes: 1

Views: 37

Answers (1)

Edward
Edward

Reputation: 896

This works by first copying your list of business entities, then removes those that has any matches in the excludedName/excludeZip pair.

search_copy=search_results[:]
for j in search_results:
    for i in range(0,len(excludeName)):
        if (j['name'] == excludeName[i]) and (j['location']['zip_code'] == excludeZip[i]):
            search_copy.remove(j)

In theory for best performance you want to iterate the bigger list first, which I would assume is the list of businesses

Upvotes: 1

Related Questions