newbie question: for loop within for loop confusion

John Salerno johnjsal at
Mon Jun 16 05:11:08 CEST 2008

takayuki wrote:

> 		for letter in avoid:
> 			if letter in word:
> 				break
> 			else:
> 				print word

Take the word 'dog', for example. What the above loop is doing is 
basically this:

1. for letter in avoid uses 'a' first
2. is 'a' in 'dog'?
3. no, so it prints 'dog'
4. go back to for loop, use 'b'
5. is 'b' in 'dog'?
6. no, so it prints 'dog' again
7. go back to for loop.....

Since it goes sequentially through 'abcd', it will say that the first 
three letters are not in 'dog', and therefore print it three times. Then 
it finally sees that 'd' *is* in dog, so it skips it the fourth time 
through the loop.

More information about the Python-list mailing list