Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I have tried so many things, such as unzipping and

for elem in mylist:

     mylist.append(elem)

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

and even

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

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']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading