How to cleanly loop over two files in parallel in Python
python
Solution
You can try to use enumerate,
http://docs.python.org/tutorial/datastructures.html#looping-techniques
lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
for i,line in enumerate(lines):
v = [eval(s) for s in line.split()]
if v[0] > v[1]:
print words[i].strip()
Problem
I frequently write code like: ``` lines = open('wordprob.txt','r').readlines() words = open('StdWord.txt','r').readlines() i = 0 for line in lines: v = [eval(s) for s in line.split()] if v[0] > v[1]: print words[i].strip(), i += 1 ``` Is it possible to avoid use variable i and make the program shorter? Thanks.