-1

If I had a nested list:

Nes = [[2, 2], [4, 4], [8, 8], [16, 16]]

Would there any possible way to "unnest" it by making all the brackets inside the list go away so that Nes now looks like:

Nes = [2, 2, 4, 4, 8, 8, 16, 16] 
J. Doe
  • 1,475
  • 2
  • 9
  • 17

2 Answers2

0

You can use list comprehension:

Nes = [[2, 2], [4, 4], [8, 8], [16, 16]]
print([i for sublist in Nes for i in sublist])

This outputs:

[2, 2, 4, 4, 8, 8, 16, 16]

Please refer to the official documentation on Nested List Comprehensions for details.

blhsing
  • 91,368
  • 6
  • 71
  • 106
0

use itertools

from itertools import chain

Nes = [[2, 2], [4, 4], [8, 8], [16, 16]]

list(chain.from_iterable(Nes))

output:

[2, 2, 4, 4, 8, 8, 16, 16]
H.Bukhari
  • 1,951
  • 3
  • 10
  • 15