Reputation: 37
I have 2 lists and an output file sent to a function I am having an issue with how to do the .write statement.
I have tried using an index, but get list error.
nameList = ['james','billy','kathy']
incomeList = [40000,50000,60000]
I need to search the lists and write the name and income to a file.
for income in incomeList:
if income > 40000:
output.write(str("%10d %12.2f \n") # (this is what I can't figure out)))
Upvotes: 0
Views: 111
Reputation: 2227
You can do it like this.
nameList = ['james','billy','kathy']
incomeList = [40000,50000,60000]
for k, v in zip(nameList, incomeList):
if v > 40000:
print(k,v )
Output :-
billy 50000
kathy 60000
Upvotes: 1
Reputation: 55
In the case of 2 lists, I would suggest using a dict.
nameIncomeList = {'james':40000,'billy':50000,'kathy':60000}
For multiple case scenario,
f=open('f1.txt','w')
for i in range(len(nameList)):
if incomeList[i]>40000:
f.write(nameList[i]+' '+incomeList[i]+'\n')
f.close()
Upvotes: 0
Reputation: 972
Maybe this is what you want:
for i,income in enumerate(incomeList):
if income > 40000:
output.write(str(nameList[i]) )
Upvotes: 1