The problem with your code is that you effectively have 2 loops, one that goes through x
from 0
to the length of the list and another one that goes through each line in the list so it looks like:
for x in range(len(list1)):
for line in list2:
Finallist.append("|".join((str(list1[x]),line)))
So you will be appending every line, on each iteration through list1
. You probably wanted something like:
for x in range(len(list1)):
Finallist.append("|".join((str(list1[x]),list2[x])))
Here's how to do it more compactly with a list comprehension:
>>> list1 = [0, 1, 2]
>>> list2 = ['Start|983471|String1|True\n',
'Start|983472|String2|True\n',
'Start|983473|String3|True\n']
>>> ['{0}|{1}'.format(num, s) for num, s in zip(list1, list2)]
['0|Start|983471|String1|True\n', '1|Start|983472|String2|True\n', '2|Start|983473|String3|True\n']
I observe you are just using the index of each line, so instead you could use:
['{0}|{1}'.format(i, s) for i, s in enumerate(list2)]