Python Forum
problem about 'if' and 'for' from a python beginner - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problem about 'if' and 'for' from a python beginner (/thread-11173.html)



problem about 'if' and 'for' from a python beginner - yzjnpu - Jun-26-2018

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!


RE: problem about 'if' and 'for' from a python beginner - buran - Jun-26-2018

Because you change new_accounts list while iterating over it by removing ada from it


RE: problem about 'if' and 'for' from a python beginner - nilamo - Jun-26-2018

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.


RE: problem about 'if' and 'for' from a python beginner - buran - Jun-26-2018

Have a look at sets. This could be done in couple of lines using sets