Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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
by (106k points)

To flatten a list of lists you can 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]]))

Related questions

0 votes
1 answer
asked Sep 11, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
+3 votes
2 answers

Browse Categories

...