I have two lists, I need to get matching items as sets.
list1 = [1,2,3]
list2 = [1,2,3,4]
a = [(x, y) for x in list1 if x in [y for y in list2]]
It gets X correctly but as expected it just gives the first item in nested list(y) instead of one that matched.
What is the easiest way to make this work and get X and Y that match in set? Any way to avoid using regex?
Update: To make it more clear, above is just example - the actual code is:
list1 = [(x, y) for x in new_ids if x.KWuser in [y.keyword for y in get_existing_KeyO]]
new_ids and get_existing_KeyO are lists of items from 2 different models:
get_existing_KeyO = list(KeyO.objects.filter(
keyword__in=[x['keyword'] for x in related_data]).all())