5

Let's say i have a list that consist of:

[['a','b','c','d']]

Is there an easy way to remove the outer list so the output would be

['a','b','c','d']

I figured something like pop, but that remove the element, and i just want to remove the outer list.

I know i could iterate over the double list and append the elements to a new list, and the problem would be solved, but im not happy with that solution, i want a smoother one with cleaner code.

PlayPhil1
  • 191
  • 1
  • 3
  • 6
  • "I figured something like pop, but that remove the element, and i just want to remove the outer list." what would be the difference? – juanpa.arrivillaga Aug 31 '17 at 17:02
  • 1
    While it's probably not useful for most cases, there is a way to *directly* rewrite the existing `list` without the nesting, rather than extracting it, so other references to the `list` are modified as well. `mylist[:] = mylist[0]` would replace the contents of `mylist` with the contents of the nested list without changing the identity of `mylist`. – ShadowRanger Oct 27 '17 at 21:07

1 Answers1

21

You can use unpacking:

[new_list] = [['a','b','c','d']]
print(new_list)

Output:

['a','b','c','d']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102