Ness
Ness

Reputation: 168

List of tuples of 2 elements using a list comprehension

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.

Upvotes: 0

Views: 52

Answers (1)

David
David

Reputation: 8318

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

Upvotes: 2

Related Questions