Skip to content
Advertisement

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:

JavaScript

But that gives me an error:

JavaScript

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:

JavaScript

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement