I am a newbie to python and coding, and I was writing this script where I wanted to get the intersection (matched items) in two very long lists, each over 200000 item. I used two for loops as follows:
for x in list1:
for y in list2:
print(y)
But it took a run-time over one hour to get the desired result. I got an idea about turning the two lists into sets, and then using the intersection operand & on them, and it worked perfectly, the run-time was reduced to only three seconds.
My question is: how is that possible? How do these operands work so fast? Don't they also need to iterate through all the items in both sets?