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 a string and concatenate values from a list

lstA looks like this:

["y1","y2","y3"]

and lstB looks like this:

["xx5, folder, 20-1-1", "xx6, appPath, 20-1-1", "xx7, Resolve, 23-1-4"]

All items are strings. Lengths of the lists are the same.

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

I want to iterate through each item of lstB and replace the first item before , with the element from lstA respectively.

At the end, lstB should look like this:

["y1, folder, 20-1-1", "y2, appPath, 20-1-1", "y3, Resolve, 23-1-4"]

First, I can iterate through lstB, split the string by , to obtain the value of the first item (for example "xx5"). Then, I can replace this with the respective element in lstA. However, I am not sure how I can replace since lstA is separate.

for i in lstB:
    toBeReplaced = i.split(',')[0]
    totalName = i.replace(toBeReplaced, lstA[i])

>Solution :

very simplified method if you’re new to programming.

  • code:
a = ["y1","y2","y3"]
b = ["xx5, folder, 20-1-1", "xx6, appPath, 20-1-1", "xx7, Resolve, 23-1-4"]
List = []
for i in range(len(a)):
    temp = b[i].split(',')
    temp[0] = a[i]
    List.append((',').join(temp))
  • output:
    ['y1, folder, 20-1-1', 'y2, appPath, 20-1-1', 'y3, Resolve, 23-1-4']
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