I want to execute two for loops at the same time in python, in order to read at the same time two lines with the same index in two different files.
This is what i tried:
def load_dataset(train_path: str, label_path: str) -> Tuple[List[str], List[str]]:
words = []
labels = []
with open(train_path, encoding='utf8') as tp, open(label_path, encoding='utf8') as lp:
for line_tp in tp, line_lp in lp:
line_tp = line_tp.strip()
line_lp = line_lp.strip()
if line_tp and line_lp:
word = line_tp
label = line_lp
words.append(word)
labels.append(label)
return words, labels
But, when i tried to call the function
(train_list, label_list) = load_dataset(train_path, label_path)
I get:
UnboundLocalError: local variable 'line_lp' referenced before assignment
Now, i really think that the problem is that the first for loop executes before the second loop, is there a way to execute them at the same time, with the same index?
zipto "iterate on two(or more) things simultaneously".