Sometimes we need to detect whether an element already exists in the list and is case insensitive, e.g., the list already has the element Mary, then we want to assume that MARY is also occupied. This example will be used a lot in real programming, such as to ensure that the website registration user is unique, email name is unique and so on.
The following columns will be implemented to make the list elements unique:
Create a list with several elements named current_users.
Create another list containing several elements named new_users, bi make sure that one or two of them are contained in the list current_users.
Iterates through the elements of the new_users list, testing to see if they are being used. If so, prints that the username is already in use; otherwise, prints that the username is valid.
current_users = ['lily', 'John', 'mary', 'maria', 'admin'] new_users = ['david', 'Admin', 'JOHN', 'emma'] for new_user in new_users: if new_user.lower() in [current_user.lower() for current_user in current_users]: print("The setup user name has been used, please change to another user name.") else: print('Hello! The username you set is valid')
The output result is:
hello (polite)!The username you set is valid Setting the user name has been used,Please change to another username Setting the user name has been used,Please change to another username hello (polite)!The username you set is valid
The output recognizes that John and admin are the same in the Admin and JOHN and current_users lists, and achieves the function of making the list elements unique (case-insensitive).
The above article to solve the problem of Python list characters are not case-sensitive is all I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.