[Solved] Python – why is this wrong? [closed]


Luckily my crystal ball is working today so I can guess what you mean when you say it isn’t working. Of course, you might have made it easier by actually explaining, but there we go.

If you just want a list of (x, y) pairs then zip is the way to go. The syntax you have does something else: for each element in targets it iterates completely through all elements in tmplist. This is exactly equivalent to:

 for x in targets:
    for y in tmplist:
        value.append((x, y))

So for a pair of lists [‘a’, ‘b’, ‘c’] and [1, 2, 3] you would get:
[(‘a’, 1), (‘a’, 2), (‘a’, 3), (‘b’, 1), (‘b’, 2), (‘b’, 3), (‘c’, 1), (‘c’, 2), (‘c’, 3)]

solved Python – why is this wrong? [closed]