'for loop double integers as a result

Trying to wrap my head around this and can't fix the issue. Basically i have a api_list that looks like:

[('A332', 'ZZ334', '14000', '369', '249', '52.7744', '-1.5487', 'BZZ', 'BZZ', '80.7')]

database_list that looks like:

[(204, 'A332', 'ZZ334', 15200, '390', 259, '53.1756', '0.5847', 'BZZ', 'BZZ', 28.32), (202, 'A332', 'ZZ334', 16300, '394', 259, '53.1967', '0.7807', 'BZZ', 'BZZ', 28.32)]

Where first element is an ID from database.

and nested for loops to check if there is no match.

list_to_insert = []
for x in api_list:
    for y in database_list:
        if database_list[0] == None:
            list_to_insert.append(x)
        else:
            if x[1] not in y[2]:
                list_to_insert.append(x)

This basically checks if from api_list value 'ZZ334' is not in database list index 2 then append x to list.

Problem is that it keep appending value it multiple times.

Any ideas?

I've tried to do without nested loops but then getting Error as i have to check if database[0] is not None..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source