Python Forum

Full Version: problem about 'if' and 'for' from a python beginner
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The python code is as below:
curren_accounts=['amy','ADA','eric','tim','jasmin']
new_accounts=['torge','tom','ada','ace','amanda']
for new_account in new_accounts:
	for curren_account in curren_accounts:
		if new_account.title() == curren_account.title():
			print('"'+new_account+'"'+' is already used, please find a new name.')
			new_accounts.remove(new_account)
			print(new_accounts)
	if new_account in new_accounts:
		print('"'+new_account+'"'' is not be used yet.')
the result is as below:
"torge" is not be used yet.
"tom" is not be used yet.
"ada" is already used, please find a new name.
['torge', 'tom', 'ace', 'amanda']
"amanda" is not be used yet.

So the question is why 'ace' in the new_account is not processed in the result.
Many thanks!
Because you change new_accounts list while iterating over it by removing ada from it
I'm a little surprised this sort of thing isn't in the tutorials sub-forum somewhere that can be easily linked to, like we do with the or keyword.
Have a look at sets. This could be done in couple of lines using sets