2

Say I have two lists:

>>> passwordList = ['lee', 'venter', 'rusty']
>>> wrong_num = [5, 5, 0]

I loop the two lists together to combine the two list's corresponding indexes using:

>>>for p, w in zip(passwordList,wrong_num):
>>>      newFile.write("The password " + p + " was wrong by " + str(w) + "characters")

The result looks like this:

>>> The password lee was wrong by 5 characters
>>> The password venter was wrong by 5 characters
>>> The password rusty was wrong by 0 characters

My problem comes when I want to include a index variable to make the result look like:

>>>"The password entry 1: lee, was wrong by 5 characters"
>>>"The password entry 2: lee, was wrong by 5 characters"
>>>"The password entry 3: lee, was wrong by 5 characters

I want to index the first list so I can display 1, 2 and 3 for every item in the list. So for this I include another loop that makes the code look like this:

>>>for ind, item in enumerate(passwordList):
>>>        for p, w in zip(passwordList,wrong_num):
>>>             newFile.write("The password entry " + str(ind) + ": " + p + "is wrong by " + str(w) + " characters")

The result in the file newFile looks like this:

>>> Incorrect password 1: lee, wrong by 5 characters
>>> Incorrect password 1: lee, wrong by 5 characters
>>> Incorrect password 1: lee, wrong by 0 characters
>>> Incorrect password 2: venter, wrong by 5 characters
>>> Incorrect password 2: venter, wrong by 5 characters
>>> Incorrect password 2: venter, wrong by 0 characters
>>> Incorrect password 3: rusty, wrong by 5 characters
>>> Incorrect password 3: rusty, wrong by 5 characters
>>> Incorrect password 3: rusty, wrong by 0 characters

How can I stop this from happening and rather only print:

>>> Incorrect password 1 : lee, wrong by 5 characters
>>> Incorrect password 2 : venter, wrong by 5 characters
>>> Incorrect password 3: rusty, wrong by 0 characters

This is just for the theory, so please ignore that "rusty is wrong by 0 characters" still says incorrect password at the beginning of the sting.

1 Answer 1

4

Enumerate the result of zip() instead, leaving only one for loop:

>>> passwordList = ['lee', 'venter', 'rusty']
>>> wrong_num = [5, 5, 0]
>>> for i, (p, w) in enumerate(zip(passwordList,wrong_num)):
...   print "The password entry " + str(i) + ": " + p + "is wrong by " + str(w) + " characters"
...
The password entry 0: leeis wrong by 5 characters
The password entry 1: venteris wrong by 5 characters
The password entry 2: rustyis wrong by 0 characters

Another option is to make use of itertools

>>> from itertools import izip, count
>>> passwordList = ['lee', 'venter', 'rusty']
>>> wrong_num = [5, 5, 0]
>>> for i, p, w in izip(count(), passwordList, wrong_num):
...   print "The password entry " + str(i) + ": " + p + "is wrong by " + str(w) + " characters"
...
The password entry 0: leeis wrong by 5 characters
The password entry 1: venteris wrong by 5 characters
The password entry 2: rustyis wrong by 0 characters

If you need the indexes as 1 2 3 instead of 0 1 2 you can replace str(i) with str(i+1)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much Tim, it was exactly what I needed! But one question please: Why when I try the bottom intertools, an error says: no module named intertools?
It's itertools, not intertools
Ag, please excuse Tim, misread. Thanks again, I appreciate your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.