-1

I want to see if a new username already exists in a list of users, but for the existing users to also be evaluated user the lower() function.

I have already tried this:

for nuser in new_users:
        if nuser.lower() not in users.lower():

But it does not like the users.lower():

I have also tried changing my nuser.lower() to nuser.title(), but this is not future proof as I not all users will be writing in title() format.

My whole code below:

users = ['John', 'Kyle', 'Lily', 'admin', 'Tom']
new_users = ['john', 'Mike', 'TOM', 'Daemon']

if new_users:
    for nuser in new_users:
        if nuser.lower() not in users.lower():
            print("Username: "+nuser+" is available.")
        else:
            print("Username: "+nuser+" is not available, you will have to choose another.")

For example I expect TOM and Tom to clash and print that the username is not available.

Many thanks!

roganjosh
  • 12,594
  • 4
  • 29
  • 46
Kyle
  • 13
  • 2

2 Answers2

1
users = ['John', 'Kyle', 'Lily', 'admin', 'Tom']
new_users = ['john', 'Mike', 'TOM', 'Daemon']

if new_users:
    users_lower = set(map(str.lower, users))
    for nuser in new_users:
        if nuser.lower() not in users_lower:
            print("Username: "+nuser+" is available.")
        else:
            print("Username: "+nuser+" is not available, you will have to choose another.")

users is a list it has no attribute lower - you must lower the list elements your self and use a set for lookups to be fast (element in some_list is much slower than element in some_set)

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
0

users = ['John', 'Kyle', 'Lily', 'admin', 'Tom'] New_users = ['john', 'Mike', 'TOM', 'Daemon']

Why not lower using list comprehension as follow :

users_lower= [x.lower() for x in users]

And compare New_users lowered to match in that new lowered users list ?