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.
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']