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]
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]
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.
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]