0 votes
1 view
ago in Python by (19.5k points)

I'm having a problem with square brackets in Python. I wrote a code that produces the following output:

[[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]]

But I would like to perform some calculations with that, but the square brackets won't let me.

How can I remove the brackets? I saw some examples to do that but I could not apply them to this case.

1 Answer

0 votes
ago by (46.1k points)

For python flatten list you should use itertools.chain - this will also cater for > 1 element in each sublist:

from itertools import chain 

list(chain.from_iterable([[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]]))

Welcome to Intellipaat Community. Get your technical queries answered by top developers !