简体   繁体   中英

How do I check for the same words in two different lists?

How can I check for the same words in two different lists if the words have different lower and uppercase letters? I wrote this:

current_users=["michael", "peta", "jennifer", "clark", "tony"]
new_users=["jeffry", "PETA", "shuff", "TOny", "guy"]

for new_user in new_users:

    if new_user.lower() in current_users:
        print("Sorry, the name "+new_user+" is already taken.")
    else:
        print("The name "+ new_user+ " is available.")

But it only works if the first list is in lowercase. Can this code be easily modified to disregard differences in case?

You can use list comprehensions .

Example

 if new_user.lower() in [user.lower() for user in current_users]:
if any(new_user.lower() == user.lower() for user in current_users)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM