How can I make my new list case sensitive while comparing with another list

I’m a newbie to programming! I want to make the new_users case sensitive so that second name in the list, will be flagged as one of the current_users.

#Unique name for users! 

current_users = ['paul', 'babel', 'ade', 'jackie', 'josh']
new_users = ['kiwasaki', 'Babel, 'ade', 'roy', 'ronnie']

for new_user in new_users:
    if new_user in current_users:
        print ("Try another username pls")
    else:
        print("The username is available")

So basicly what you are trying to do
Is compare list A to list B
If list B contains a name that is already in list A
It will alert
Here’s a possible solution down bellow

[spoiler]
     listA= ['chicken','rabbit','cat','dog','cow']
     listB = ['rabbit','horse','rooster']
     set(listA) - set(listB)
set(['rabbit', 'dog'])
[/spoiler]

No, what I’m looking for is something like, I have list of existing users on website, I also have another list for new users. I now want a situation whereby once there’s an existing name it will prompt (“Username is not available”). But, my point is let’s say I have

existing_user = [‘chicken’, ‘goat’, 'sheep]
new_user = [‘snake’, ‘Sheep’, ‘goat’]

I now want to merge new names to those one in the existing list. Definitely, it won’t add ‘goat’ again, but it will add ‘Sheep’ because of the capital ‘S’. This is want I’m trying to rectify. I want the new list to turn all the elements to lower case before comparing with the existing list. Thanks

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

You can use .lower() to make a string lowercase.

As said, don’t you just lower case the strings before the compare?

I don’t really know Python so this is just an example, there might be much better ways.

current_users = ['paul', 'babel', 'ade', 'jackie', 'josh']
new_users = ['kiwasaki', 'Babel', 'ade', 'roy', 'ronnie']

current_users_lower = [current_user.lower() for current_user in current_users]

for new_user in new_users:
    if new_user.lower() in current_users_lower:
        print(new_user, "Try another username pls")
    else:
        print(new_user, "The username is available")

https://docs.python.org/3/library/stdtypes.html#string-methods
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

1 Like

That’s what I was thinking, I just didn’t want to type that out on my phone!

Though I wonder what the most performant Pythonic approach would be for case insensitive matching with large lists. I don’t think that a full second copy would be preferred then, but I’m not sure of the best way.

I would have thought a regex match would be more performant. Also, seems like the situation where regex was designed (usernames/passwords).

current_users = ['paul', 'babel', 'ade', 'jackie', 'josh']
new_users = ['kiwasaki', 'Babel','ade', 'roy', 'ronnie']
for i in new_users:
    if i not in current_users:
        current_users.append(i)
    else:
        print("{} user name already exist".format(i))
print(current_users)

Try this out, I think this is what you want.Here is the ouput

It doesn’t give answer to my question. If you look at the new list, you’ll discover that Babel and babel appears just because of the case, and it might be for the same user.

Did you try using .lower?

For that you can use .lower() function as many other suggested here, and it ll convert all text in lower case.If you want to preserve the case of the username(meaning Babel will remain Babel )and also make it case sensitive ,here is the code

current_users = ['paul', 'babel', 'ade', 'jackie', 'josh','Bear']
new_users = ['kiwasaki', 'Babel','ade', 'roy', 'ronnie','bear']
for i in new_users:
    if i.lower() not in [j.lower() for j in current_users]:
        current_users.append(i)
    else:
        print("{} user name already exist".format(i))
print(current_users)

I put a new element Bear, just to check al possibiities

Output:
Babel user name already exist
ade user name already exist
bear user name already exist
[‘paul’, ‘babel’, ‘ade’, ‘jackie’, ‘josh’, ‘Bear’, ‘kiwasaki’, ‘roy’, ‘ronnie’]