-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,174
  • 1
  • 7
  • 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
  • 77,832
  • 6
  • 59
  • 90
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,543
  • 1
  • 9
  • 15