-1

What would be the most pythonic way to convert the below list of lists into a single list?

[['MyValue1'], ['MyValue2'], ['MyValue3']]

Desired output"

['MyValue1', 'MyValue2', 'MyValue3']

Thank you in advance!

Baobab1988
  • 639
  • 8
  • 24
  • 2
    Duplicate of: https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists – tjallo Jun 30 '20 at 12:29

2 Answers2

0

You can use a list comprehension:

lst = [['MyValue1'], ['MyValue2'], ['MyValue3']]
lst = [n[0] for n in lst]

print(lst)

Output:

['MyValue1', 'MyValue2', 'MyValue3']
Ann Zen
  • 25,080
  • 7
  • 31
  • 51
-1

Assuming every element of my_list is a list of length 1, you can try this:

my_list = [elt[0] for elt in my_list]
Adam Boinet
  • 444
  • 6
  • 16
  • Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian Jul 01 '20 at 01:12