1

I'm looping through 2D list combined from list1 and list2 with itertools. I want to check every value if exists from list1 with every value from list2, so it looks like this:

if(list1[0] == list2[0])
if(list1[0] == list2[1])
if(list1[0] == list2[3])
if(list1[1] == list2[0])
.
.
.
.

If the value is found then continue looping but with list1[+1].

So

if list1[0] == list2[3] = True

I want to start with list[1] again and try it, by skipping the rest of list1[0] comparing.

I tried to explain the best, maybe the code will help you understand.

import itertools


list1 = ["value1","value2","value3","value4"]
list2 = ["value5","value6","value1","value8"]


mylist = list(itertools.product(list1, list2))


for a,b in mylist:
    if (a == b):
       print ("Found word " + str(a) + " in list2)
       continue

So now, after it's found, it needs to continue looping but with a[+1]. However it could work in "classic" loop, but I'm using 2D looping. Basically, I want to continue with "a" comparing to "b" again.

2
  • This sounds like an X/Y problem. Are you in fact trying to find common elements between two lists? Commented Jul 23, 2019 at 14:35
  • Sure, either to use them as two lists to compare or by making one list with two of them inside like as in the code. Commented Jul 23, 2019 at 15:35

1 Answer 1

1
for x in l1:
    l2.remove(x)

this will remove all values from l2 that appear in l1

Sign up to request clarification or add additional context in comments.

3 Comments

The values aren't equal. There is a possible value from list1 which may appear in list2. Because lists are so big, I want to check if the value from list1 appear in list2 so I can delete it. I'm doing a comparison.
so then why don't you loop through l1 and use .remove() on l2 for every element in l1. This will leave you with the list of all elements that don't appear in l1. See my updated post
"Removing" them is a sample, there is much more to explain behind this. Removing them isn't solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.