-3

Currently I have a list of lists

l=[['asd'],['fgd']].

I want to turn it into a list

goal_list=['asd','fgd']

but I do not know how to turn a list into the value inside it - does anyone have an idea how to this efficiently?

Ricefan
  • 1
  • 1

2 Answers2

0

This is a perfect use-case for itertools.chain, where we essentially chain all sublists together to flatten them!

from itertools import chain

def single_list(arr):
    return list(chain(*arr))

print(single_list([['asd'],['fgd']]))
print(single_list([['asd', 'abcd'],['fgd', 'def']]))

The output will be

['asd', 'fgd']
['asd', 'abcd', 'fgd', 'def']
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
-1

Just use a list comprehension with two loops:

goal_list = [item for sublist in l for item in sublist]
ruohola
  • 21,987
  • 6
  • 62
  • 97