0

With:

A = [7, 15, 21]
print [i, i+1, i+2 for i in A]

I'd like to get [7, 8, 9, 15, 16, 17, 21, 22, 23].

Of course like this it doesn't work, and [[i, i+1, i+2] for i in A] gives [[7, 8, 9], [15, 16, 17], [21, 22, 23]] which is not what I want.

What's the pythonic way to do this?

Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

1

You can use a double loop inside your list comprehension:

A = [7, 15, 21]
B = [b for i in A for b in (i, i + 1, i + 2)]
# [7, 8, 9, 15, 16, 17, 21, 22, 23]
Delgan
  • 18,571
  • 11
  • 90
  • 141