1
current_users = ['mohammad', 'shervin' , 'ali', 'oskol' , 'pro_gamer']
new_users = ['ali', 'ahmad', 'shervin', 'lara', 'helena']
current_users.lower()
new_users.lower()
for new_user in new_users:
if new_user in current_users:
        print(new_user + " You must change your user name.")
else:
print("user name is available")

I need to know how to do case insensitive for this lists?

2 Answers 2

1

You need to apply the method to the individual strings in the list, not the list itself.

current_users = [user.lower() for user in current_users]

UPDATE

An alternate approach is to use sets which can figure out containment faster than your own loop:

current_users = ['mohammad', 'shervin' , 'ali', 'oskol' , 'pro_gamer']
new_users = ['ali', 'ahmad', 'shervin', 'lara', 'helena']

# normalize and convert to sets
current_users = set(user.lower() for user in current_users)
new_users = set(user.lower() for user in new_users)

# use sets to make decisions
conflicts = current_users & new_users
print("users {} already exist".format(', '.join(conflicts)))
uniques = new_users - current_users
print("users {} are available".format(', '.join(uniques)))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks lot can you tell me how to new_users in current_users: if 'Shervin' used 'shervin' must doesn't accept.
Since you lower cased "Shervin" to "shervin", you'll catch it.
It is all i need thank you so much man. i am sorry i can't vote cause my reputation is under 15.
0

.lower() can only be applied to strings.

Try this:

current_users = ['mohammad', 'shervin', 'ali', 'oskol', 'pro_gamer']
new_users = ['ali', 'ahmad', 'shervin', 'lara', 'helena']

current_users = [user.lower() for user in current_users]

for new_user in new_users:
  if new_user in current_users:
  print(new_user + " You must change your user name.")
else :
  print("user name is available")

3 Comments

This one really help me but can you tell me how to check new_users in current_users like this: if 'shervin' used 'Shervin' must doesn't accept.
Um, did you just cut and paste my fix for this?!
Consider rewording that a little. .lower() can be applied to any class that has a lower method. list doesn't, but others do.

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.