Split The Second String of Every Element in a List into Multiple Strings


I am very very new to python so I’m still figuring out the basics.

I have a nested list with each element containing two strings like so:

mylist = [['Wowza', 'Here is a string'],['omg', 'yet another string']]

I would like to iterate through each element in mylist, and split the second string into multiple strings so it looks like:

mylist = [['wowza', 'Here', 'is', 'a', 'string'],['omg', 'yet', 'another', 'string']]

I have tried so many things, such as unzipping and

for elem in mylist:


     NewList = [item[1].split(' ') for item in mylist]

and even

for elem in mylist:
   NewList = ' '.join(elem)
   def Convert(string):
       li = list(string.split(' '))
       return li 

Which just gives me a variable that contains a bunch of lists

I know I’m way over complicating this, so any advice would be greatly appreciated

>Solution :

You can use list comprehension

mylist = [['Wowza', 'Here is a string'],['omg', 'yet another string']]
req_list = [[i[0]]+ i[1].split() for i in mylist]
# [['Wowza', 'Here', 'is', 'a', 'string'], ['omg', 'yet', 'another', 'string']]

Leave a Reply Cancel reply