Using while loop to move items between two lists in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

It is very often that items in list should be moved to another list in Python programming. For example, users trying to register on a website can only be moved to a verified user list after certain conditions are met. In such circumstance, a while loop can be applied.

#a list of new users waiting to verify
new_users = ['Wilson', 'Mico', 'Shirley', 'Dudu', 'Miaomiao','dudu']
#en empty list for verified user
verified_users = []
#while loop to verify every new user
#if the lower form of the new user is same as one of the user in 
#verified user, then this user can not go into verified user list
while new_users:
    new_user = new_users.pop()
    
    if new_user.lower() not in verified_users:
        verified_users.append(new_user.lower())
    else:
        print(new_user + " is already in the list")
#output
Dudu is already in the list
# Display all verified users.
print("\nThese are all the verified users now:")
for verify_user in verified_users:
    print(verify_user)
#output
These are all the verified users now:
dudu
miaomiao
shirley
mico
wilson

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago