Flatten list of lists

323,712

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]
Share:
323,712
Hellfish
Author by

Hellfish

Master student. X-ray Diffraction.

Updated on February 17, 2020

Comments

  • Hellfish
    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
    Platinum Azure almost 12 years
    Using itertools.chain.from_iterable is much simpler.
  • Paul Seeb
    Paul Seeb almost 12 years
    Why would you import an entire module to do something as simple as this?
  • Paul Seeb
    Paul Seeb almost 12 years
    Please explain why. This is about as simple as it gets
  • Jon Clements
    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
    Platinum Azure almost 12 years
    Sorry, 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
    Paul Seeb almost 12 years
    if 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
    Platinum Azure almost 12 years
    Unfortunately, chain has the same shortcoming as a list comprehension with regard to many levels of nesting (that is, it is not recursive). However, I believe itertools 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
    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
    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
    Jon Clements almost 12 years
    @PlatinumAzure Do-able but ugh - Think I'll take a rain check on that one though ;)
  • estani
    estani over 11 years
    +1 for being more than twice faster (time it), requires no import and is more compact-
  • estani
    estani over 11 years
    The 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
    Cerin over 10 years
    Unfortunately, this doesn't distinguish between lists and strings.
  • Jon Clements
    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
    ahoffer almost 10 years
    Love this answer, but I can't figure it out. Can you talk me through how Python evaluates this list comprehension?
  • Paul Seeb
    Paul Seeb almost 10 years
    @ahoffer I made some updates. Let me know if that clarifies this. Thanks for the suggestion.
  • ahoffer
    ahoffer almost 10 years
    @Paul. Fantastic. Seeing it broken into blocks was enlightening.
  • travc
    travc about 9 years
    While 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
    len over 3 years
    I like your answer very much, but how can flatten [1,[2,3]] ?
  • LarsH
    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
    Merlin about 2 years
    This is successful for nested lists of uniform depth, but not something like ['apple', 'pie', ['ham', 'cheese']]