Flatten list of lists
Solution 1
Flatten the list to "remove the brackets" using a nested list comprehension. This will un-nest each list stored in your list of lists!
list_of_lists = [[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]]
flattened = [val for sublist in list_of_lists for val in sublist]
Nested list comprehensions evaluate in the same manner that they unwrap (i.e. add newline and tab for each new loop. So in this case:
flattened = [val for sublist in list_of_lists for val in sublist]
is equivalent to:
flattened = []
for sublist in list_of_lists:
for val in sublist:
flattened.append(val)
The big difference is that the list comp evaluates MUCH faster than the unraveled loop and eliminates the append calls!
If you have multiple items in a sublist the list comp will even flatten that. ie
>>> list_of_lists = [[180.0, 1, 2, 3], [173.8], [164.2], [156.5], [147.2], [138.2]]
>>> flattened = [val for sublist in list_of_lists for val in sublist]
>>> flattened
[180.0, 1, 2, 3, 173.8, 164.2, 156.5, 147.2,138.2]
Solution 2
I would 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]]))
Solution 3
Given
d = [[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]]
and your specific question: How can I remove the brackets?
Using list comprehension :
new_d = [i[0] for i in d]
will give you this
[180.0, 173.8, 164.2, 156.5, 147.2, 138.2]
then you can access individual items with the appropriate index, e.g., new_d[0]
will give you 180.0
etc which you can then use for math.
If you are going to have a collection of data, you will have some sort of bracket or parenthesis.
Note, this solution is aimed specifically at your question/problem, it doesn't provide a generalized solution. I.e., it will work for your case.
Solution 4
>>> lis=[[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]]
>>> [x[0] for x in lis]
[180.0, 173.8, 164.2, 156.5, 147.2, 138.2]
Comments
-
Hellfish over 4 years
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 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.
-
Platinum Azure almost 12 yearsUsing
itertools.chain.from_iterable
is much simpler. -
Paul Seeb almost 12 yearsWhy would you import an entire module to do something as simple as this?
-
Paul Seeb almost 12 yearsPlease explain why. This is about as simple as it gets
-
Jon Clements almost 12 years@PaulSeeb simplest, generic, built-in method way of flattening an iterable of iterables... What if the sublists had 100 elements? I certainly wouldn't want to be typing the listcomp code for that one...
-
Platinum Azure almost 12 yearsSorry, I meant more readable/intuitive. Since the OP is having trouble disambiguating between what lists are (sequences of data) and how lists are represented in code (with square brackets), I highly doubt a double list comprehension will make any sense to him/her.
-
Paul Seeb almost 12 yearsif the sublist had 100 elements a nested list comp could still deal with this without importing an entire module. It is mostly just style and preference but personally I see the list comp as more readable as most programmers have used list comps but not all have read the entire itertools library. Not that its difficult to look up but its just one more thing.
-
Platinum Azure almost 12 yearsUnfortunately,
chain
has the same shortcoming as a list comprehension with regard to many levels of nesting (that is, it is not recursive). However, I believeitertools
allows for many nice operations to be done with highly readable code, which is why I still support this answer rather than using a double list comprehension. -
Jon Clements almost 12 years@PlatinumAzure I was in the middle of writing that it breaks when more than 1-level deep when the phone rang - so you beat me to it that time! I was also going to mention that if the list doesn't need to be materialised, then just looping over the iterator that chain generates could be an advantage...
-
Platinum Azure almost 12 years@JonClements Might as well. In my answer, I had been about to write an example for summing the elements using
reduce
. Go ahead and run with that or something similar :-) -
Jon Clements almost 12 years@PlatinumAzure Do-able but ugh - Think I'll take a rain check on that one though ;)
-
estani over 11 years+1 for being more than twice faster (time it), requires no import and is more compact-
-
estani over 11 yearsThe OP has little idea about lists, giving him/her this solution might cause undesirable effects if s/he wants to use it to flatten multiple elements (no error, just silently return the first...)
-
Cerin over 10 yearsUnfortunately, this doesn't distinguish between lists and strings.
-
Jon Clements over 10 years@cerin Indeed. It works on purely an iterable of iterables (as do all the other answers here). So yes, a list of strings would most likely result in a non-sensible result. However, that's a different answer which can depend on Python version and wasn't part of this question which was based on lists of single floats.
-
ahoffer almost 10 yearsLove this answer, but I can't figure it out. Can you talk me through how Python evaluates this list comprehension?
-
Paul Seeb almost 10 years@ahoffer I made some updates. Let me know if that clarifies this. Thanks for the suggestion.
-
ahoffer almost 10 years@Paul. Fantastic. Seeing it broken into blocks was enlightening.
-
travc about 9 yearsWhile the question is a duplicate, I think this answer is superior to any others I've seen. I like list comprehension a lot, but this particular construct remains non-intuitive for some reason.
-
len over 3 yearsI like your answer very much, but how can flatten [1,[2,3]] ?
-
LarsH almost 3 years@travc I agree ... it seems like this nested list comprehension is intuitively backwards. Seems like it should be
[val for val in (sublist for sublist in list_of_lists)]
or something like that. But that yields the original nested list. Apparently the "nested list comprehension" here is a different structure from what I'm thinking. -
Merlin about 2 yearsThis is successful for nested lists of uniform depth, but not something like
['apple', 'pie', ['ham', 'cheese']]