1

For example, I have a list of lists:

[[['--w'], ['ww-'], ['bbb']], [['w--'], ['-ww'], ['bbb']]]

I want to remove inner one to be like this

[['--w', 'ww-', 'bbb'], ['w--', '-ww', 'bbb']]

How can I do this?

rrryy39
  • 29
  • 3
  • 1
    What have you tried yourself? What happened? What did you expect? – Grismar May 07 '21 at 01:12
  • 1
    is this a list with a single element who is a list of lists? you might be able to return the first index – DarkLeader May 07 '21 at 01:14
  • 2
    Don't change your questions after they've been asked to invalidate existing answers. Your post-change question is just an extension of [Flatten list of lists](https://stackoverflow.com/q/11264684/364696); you'd apply the answers to that question inside a `list` comp over the outermost `list` to flatten the inner `list`s. – ShadowRanger May 07 '21 at 01:36

3 Answers3

3

If this is just for the purposes of printing, you can do:

list_o_lists_o_lists = [[['--w'], ['ww-'], ['bbb']], [['w--'], ['-ww'], ['bbb']]]
print(*list_o_lists_o_lists, sep=", ")

which unpacks the outer list into its two inner lists as sequential arguments to print, which prints each separately, while sep=", " tells print to place a comma and space between each of them, getting the display you want, but making no change to the data structure.

But otherwise, what you want isn't possible as stated; removing the outer brackets leaves you with two elements that need to be stored in something. The literal with those brackets removed is a tuple:

[['--w'], ['ww-'], ['bbb']], [['w--'], ['-ww'], ['bbb']]

but the repr of a tuple includes parentheses, so you'd just be replacing the outer brackets with parens if you printed it without special formatting effort:

>>> [['--w'], ['ww-'], ['bbb']], [['w--'], ['-ww'], ['bbb']]
([['--w'], ['ww-'], ['bbb']], [['w--'], ['-ww'], ['bbb']])
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 2
    this variable naming... it hurts my eyes – Ayush Garg May 07 '21 at 01:22
  • @megargayu: Yeah, it's awful. :-) But it also makes it clear for the example what the structure is. The OP talks about removing brackets, but they're not decorative, they're the outer `list`, and they have to know that. – ShadowRanger May 07 '21 at 01:25
  • For that purpose, it would be much easier (and cleaner) to use a comment – Ayush Garg May 07 '21 at 01:27
  • For throwaway examples? Who cares? I'm definitely not suggesting they actually use this name in real code, but I haven't the foggiest notion what this data is supposed to represent, so that's about as descriptive as I can be. – ShadowRanger May 07 '21 at 01:30
  • 1
    Okay, I guess you are correct. Just for whoever reads this: DO NOT NAME YOUR VARIABLES "`list_o_lists_o_lists`"!!! Instead, use short variable names that make sense and add comments for an extra description. – Ayush Garg May 07 '21 at 01:30
0

Your description doesn't match your desired output. Your desired output has the inner brackets removed, not the outer ones. To remove the inner brackets, you can do

[[item[0] for item in inner_list] for inner_list in outer_list]
Acccumulation
  • 3,491
  • 1
  • 8
  • 12
  • which one is item and which one is inner_list? – rrryy39 May 07 '21 at 01:33
  • 1
    Sigh... They edited the question to completely change the desired output format, while leaving the description of their goal unchanged. – ShadowRanger May 07 '21 at 01:34
  • @myy39 `item` and `inner_list` are both dummy variables. They exist while iterating through a list, but don't refer to anything outside. `outer_list` refers to the entire list, and it's the only variable that has to be defined. – Acccumulation May 07 '21 at 02:01
-2

Try this

[[j[0] for j in i] for i in [[['--w'], ['ww-'], ['bbb']], [['w--'], ['-ww'], ['bbb']]]]

Output:

[['--w', 'ww-', 'bbb'], ['w--', '-ww', 'bbb']]
top talent
  • 615
  • 4
  • 17
  • 1
    origin[0] will give [['--w'], ['ww-'], ['bbb']] – rrryy39 May 07 '21 at 01:17
  • 2
    Can someexplain to me why people think this works and why they upvoted? – Buddy Bob May 07 '21 at 01:24
  • 1
    @toptalent: You could just as easily write `result1, result2 = origin` in this case, no need to build a new `tuple` just to unpack it. But it still doesn't have an *output*, you're saying you'll see it in a specific way without actually showing how you display it (now that you're unpacking, there are multiple "its" involved, so "output" makes even less sense). – ShadowRanger May 07 '21 at 01:27
  • this is some complex list comprehension... maybe split it up into different variables so its actually readable? Also, a description of what is even happening here would be nice. – Ayush Garg May 07 '21 at 01:39