-2

I work with a 3D List and want to remove the most nested brackets and comma of the list.

My original list looks like this:

[[[2.250922, 48.867699], [2.250805, 48.867744], [2.250688, 48.867699], [2.250688, 48.867611], [2.250805, 48.867566], [2.250922, 48.867611], [2.250922, 48.867699]], [[2.251038, 48.867832], [2.250922, 48.867877], [2.250805, 48.867832], [2.250805, 48.867744], [2.250922, 48.867699], [2.251038, 48.867744], [2.251038, 48.867832]]]

I like to remove the square brackets and the comma, which divides the values inside the brackets so it looks more like this:

[[2.250922 48.867699, 2.250805 48.867744, 2.250688 48.867699, 2.250688 48.867611, 2.250805 48.867566, 2.250922 48.867611, 2.250922 48.867699], [2.251038 48.867832, 2.250922 48.867877, 2.250805, 48.867832, 2.250805 48.867744, 2.250922 48.867699, 2.251038 48.867744, 2.251038 48.867832]]

I already tried to use functions for flattening the list, but in that case only the outer brackets got removed. Further, I tried to use following function:

new_list = [[item[0] for item in inner_list] for inner_list in outer_list]

Yet, in that instance I lose half of my values inside the brackets (all values with 48.xx). Is there a solution to this problem?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
sdiv
  • 31
  • 2
  • Does this answer your question? https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists – Mortz Jun 07 '22 at 12:32
  • What you want will not happen simply by changing the data structure (well, not without `numpy` getting involved, and it's only by coincidence that that works); you'll need to explicitly format the innermost `list`s. – ShadowRanger Jun 07 '22 at 12:32
  • Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) – Mortz Jun 07 '22 at 12:32
  • The desired result is syntactically incorrect (missing commas), therefore you can't create such a data structure in Python. – Michael Butscher Jun 07 '22 at 12:33
  • @Mortz: They want one layer of flattening, but they also want some formatting tweaks that flattening can't accomplish on its own, so it's not a great dupe target. – ShadowRanger Jun 07 '22 at 12:33

2 Answers2

0

try with this:

    original_list = [[[2.250922, 48.867699], [2.250805, 48.867744], [2.250688, 48.867699], [2.250688, 48.867611], [2.250805, 48.867566], [2.250922, 48.867611], [2.250922, 48.867699]], [[2.251038, 48.867832], [2.250922, 48.867877], [2.250805, 48.867832], [2.250805, 48.867744], [2.250922, 48.867699], [2.251038, 48.867744], [2.251038, 48.867832]]]
    new_list = [[num for sub_l in l for num in sub_l] for l in original_list]
    print(new_list)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '22 at 13:22
0

For nested lists, I use the following function:

def flatten(iterable):
    for item in iterable:
        if isinstance(item, (list, tuple, set)):
            yield from flatten(item)
        else:
            yield item


flat_list = list(flatten(nested_list))

If you want to keep the structure [[list_1], [list_2]] instead of [list_1, list_2], then:

semi_flat_list = [list(flatten(item)) for item in nested_list]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71