Home > Mobile >  Find item with 2 conditions in a list and append to another list
Find item with 2 conditions in a list and append to another list

Time:05-10

If I have a list like the following that gives a website and a location (number):

my_list=(['example.com',35], ['anotherexample.com',44], ['google.com',6], ['example.com',35], ['example.com',36])

I want to find all cases where the first part, the website, "example.com" matches another website, but has a different location (number)...

So for example, I want my code to output example.com because it was found in both location 35, and 36.

My code so far is:

    two_locations_list=[]
for i in range(len(my_list)):
    if my_list[i][0] in my_list[i][0] and my_list[i][1]!=my_list[i][1]:
        two_locations_list.append(my_list[i][0])

I cannot figure out the correct logic for this. Any input would be greatly appreciated.

CodePudding user response:

Try:

out = {}
for site, location in my_list:
    out.setdefault(site, set()).add(location)

out = [k for k, v in out.items() if len(v) > 1]
print(out)

Prints:

['example.com']

CodePudding user response:

Change your list to a dictionary mapping each url to all possible locations. Then filter urls which have multiple locations:

my_dict = dict()
for url, location in my_list:
    if url not in my_dict:
        my_dict[url] = set()
    my_dict[url].add(location)

output = [k for k,v in my_dict.items() if len(v)>1]

>>> output
["example.com"]
  • Related