Skip to content
Advertisement

Increment of every element in list of lists with keeping the shape of the original list [closed]

I have a list for example: [[1,2,3],[2,4],[3,5],[4,6,7]] I want to have ten repeats of the list, and every element increase by 1 than the previous element like: [[1,2,3],[2,4],[3,5],[4,6,7],[2,3,4],[3,5],[4,6],[5,7,8]................40 elements(lists)]

Advertisement

Answer

You can do this with a nested list comprehension:

JavaScript

The outermost iteration is for i in range(10), which takes you through the ten values of i that will be added to the elements of a. The next level for x in a iterates over the elements of a as x. The inner list comprehension [y+i for y in x] is what increments each x by i.

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