Can't copy lowercase version of list

Joined
Dec 12, 2022
Messages
8
Reaction score
0
I am trying to write a program that ensures new users of a website all have a unique username. To do this, I am trying to make a copy of a list that's all lowercase to check new usernames against existing usernames. I am getting an error and the problem seems to be with copying the list.
Here's my code:

Python:
current_users = ['nikki', 'sarah', 'steven', 'john', 'ellen']
new_users = ['nikki', 'matt', 'jonathan', 'eric', 'sarah']

current_users_lower = current_users.lower()

for user in current_users_lower:
    if user in current_users_lower:
        print(f"Sorry, {user}, that username is already taken.")
    else:
        print(f"Welcome, {user}.")

Any help would be appreciated.
Thanks
 
Joined
Dec 10, 2022
Messages
107
Reaction score
26
You are trying to lowercase a list. You will have to go through each element of the list to lowercase it.
Python:
current_users = ['nikki', 'sarah', 'steven', 'john', 'ellen']
new_users = ['Nikki', 'matt', 'Jonathan', 'eric', 'Sarah']

# List comp. to build a new list with all lowercase.
new_users_lower = [user.lower() for user in new_users]

for user in new_users_lower:
    if user in current_users:
        print(f'Sorry, {user}, that name is already taken.')
    else:
        print(f'Welcome {user}!')
 
Joined
Dec 12, 2022
Messages
8
Reaction score
0
You are trying to lowercase a list. You will have to go through each element of the list to lowercase it.
Python:
current_users = ['nikki', 'sarah', 'steven', 'john', 'ellen']
new_users = ['Nikki', 'matt', 'Jonathan', 'eric', 'Sarah']

# List comp. to build a new list with all lowercase.
new_users_lower = [user.lower() for user in new_users]

for user in new_users_lower:
    if user in current_users:
        print(f'Sorry, {user}, that name is already taken.')
    else:
        print(f'Welcome {user}!')
Thank-you.
 
Joined
Jul 4, 2023
Messages
507
Reaction score
63
You can use also map
in the case that list "current_users" is always contain users name in lower case
[ on-line ]
HTML:
current_users = ['nikki', 'sarah', 'steven', 'john', 'ellen']
new_users = ['Nikki', 'matt', 'sarah', 'jonathan', 'eric', 'sarah', 'nikki']

new_users_lower = map(str.lower, new_users)

for user in new_users_lower:
    if user in current_users:
        print(f"Sorry, {user}, that username is already taken.")
    else:
        print(f"Welcome, {user}.")


BTW,
[ on-line ]
Python:
current_users = ['nikki', 'sarah', 'steven', 'john', 'ellen']
new_users = ['Nikki', 'matt', 'sarah', 'jonathan', 'Matt', 'eric', 'sarah', 'nikki']

welcome = set()
taken = set()

for user in new_users:
    user = user.lower()
    if user in current_users:
        taken.add(user)
    else:
        welcome.add(user)

print("Welcome users:", welcome)
print("Taken users:", taken)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,053
Messages
2,570,431
Members
47,075
Latest member
TysonV438

Latest Threads

Top