0

How can I turn this

list = [['I','am','a','hero'],['What','time','is','it']]

to this

list = [["I am a hero"],["What time is it"]]

This doesn't work:

list(chain.from_iterable(list.str.split(',')))

And neither does:

[a for a in list]
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
Ron
  • 167
  • 6

2 Answers2

2

Here is one way of doing it

lst = [['I','am','a','hero'],['What','time','is','it']]

new_list = [[' '.join(x)] for x in lst]
print(new_list)
JacksonPro
  • 3,135
  • 2
  • 6
  • 29
2

You could join the list items:

list1 = [['I', 'am', 'a', 'hero'], ['What', 'time', 'is', 'it']]
list2 = [[' '.join(item)] for item in list1]
print(list2)

Output:

[['I am a hero'], ['What time is it']]
  • Oh? Apologies for the poor vocab. Any resources where I can read further? @wjandrea – Yaakov Bressler Dec 14 '20 at 16:10
  • 2
    @YaakovBressler, it's more nitpicking, don't take it seriously. Collision is just not right word. – Olvin Roght Dec 14 '20 at 16:15
  • Ah. Simple Wikipedia search provides clarity. @OlvinRoght – Yaakov Bressler Dec 14 '20 at 16:19
  • 1
    @Yaakov No problem! You've got the right idea. I'm not aware of any resources specifically talking about collision (I don't think Python even has collision), but for shadowing, see [this answer](https://stackoverflow.com/a/53734745/4518341) for a quick overview and [Wikipedia](https://en.wikipedia.org/wiki/Variable_shadowing) for a more technical definition, plus read about scope: [Short description of the scoping rules?](https://stackoverflow.com/q/291978/4518341) – wjandrea Dec 14 '20 at 16:28