I want to use a list comprehension to initialize a list of tuples that has a 2 elements in it, my attempt is as follows:
SIZE = 10 possible_positions = [(x, y) for x, y in range(0, SIZE)]
But that gives me an error:
TypeError: cannot unpack non-iterable int object
What is the right way to do it ? I know I can use a for loop, but I want to know anyway.
Advertisement
Answer
range
return a single value per iteration, you should use zip
combined with range
in the following way:
zip(range(SIZE), range(SIZE))
Using zip
will also save you the trouble of creating the list of tuples so calling list(zip(range(SIZE), range(SIZE)))
will give you the end result