slowlearner02
slowlearner02

Reputation: 13

Appending List Elements to Another List (Python)

I've made some code that has a list of new users and current users and checks to see if the username is available. If unavailable, it outputs a message saying so, otherwise outputs a message saying it's available. After this, I'm trying to append the elements of the new_users list to the current_users, on the condition that it doesn't already exist there. I would then like to print the current_users list to indicate the changes have occurred.

Here is my code so far - it only prints the current_users list without the changes still which is where my problem is.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

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

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')

if new_user not in current_users_lower:
    current_users_lower.append(new_user)
print(current_users_lower)

Thank you for any help in advance. I know question formatting is really important here so i've tried my best to ask it as clearly as possible.

Thanks!

Upvotes: 0

Views: 85

Answers (5)

정도유
정도유

Reputation: 559

use set operation. it makes code simple.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = set([user.lower() for user in current_users])
new_users = set([user.lower() for user in new_users])

print(f'{new_users.intersection(current_users_lower)} are unavailable')
really_new = new_users.difference(current_users_lower)
print(f'{really_new} are available')

print(current_users_lower.union(new_users))

Upvotes: 0

TheTriad
TheTriad

Reputation: 7

It's because the statement was not in the for statement.

 current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

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

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')
        current_users_lower.append(new_user)
print(current_users_lower)

Upvotes: 0

Ryan Michael Albers
Ryan Michael Albers

Reputation: 1

Your problem seems to be that you are only appending the last item of you new_users list to current_users_lower. Try adding the if statement that appends the list to your for loop.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

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

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')

    if new_user not in current_users_lower:
        current_users_lower.append(new_user)

print(current_users_lower)

Upvotes: 0

Celius Stingher
Celius Stingher

Reputation: 18377

IIUC, my advice would be to add the append as part of the for loop, while also using current_users_lower instead of current_users. All in all it would end up like this:

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

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

for new_user in new_users:
    if new_user in current_users_lower:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')
        print("Appending new user to list")
        current_users_lower.append(new_user)

print(current_users_lower)

Upvotes: 1

Cresht
Cresht

Reputation: 1040

You were simply missing an indent.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

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

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable")
    else:
        print(f'{new_user} available')
    if new_user not in current_users_lower:
        current_users_lower.append(new_user)
print(current_users_lower)

Upvotes: 1

Related Questions