Using nested loops for combining elements results in unexpected behavior in Python 3.11
I've been banging my head against this for hours... I'm stuck on something that should probably be simple. I'm trying to combine elements from two lists using nested loops, but I'm encountering unexpected behavior that I cannot seem to figure out. My goal is to create a list of tuples, where each tuple contains an element from the first list paired with every element from the second list. Here's the code I have: ```python list1 = [1, 2, 3] list2 = [4, 5, 6] result = [] for a in list1: for b in list2: result.append((a, b)) print(result) ``` The output should be a list of tuples like `[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]`. However, I noticed that sometimes my output seems to contain duplicates, which shouldn't be possible given the distinct elements in both lists. I've checked for any accidental modifications of the lists inside the loops, and they remain unchanged. To troubleshoot, I added print statements inside the inner loop: ```python for a in list1: for b in list2: print(f'Combining {a} with {b}') result.append((a, b)) ``` This shows the expected combinations, yet when I print `result`, I see duplicates like `[(1, 4), (1, 4), (2, 5), (1, 5), (2, 5)]`, which doesn't make sense. I also tried clearing the `result` list before each combination, but the behavior persists. I suspect it might be related to how the Python interpreter is handling memory or object references, but I'm not quite sure. Any insights or suggestions on where I might be going wrong with this nested loop implementation would be greatly appreciated! Thanks in advance! What am I doing wrong? For context: I'm using Python on Windows 10. Any suggestions would be helpful.