How to check whether an item exist on the list?
If then else and using the "in" operator in Python is able to check whether an item is on the list or not.
Here's a code example, it's not a complete program but a demonstration how the snippet can be used, the code can be integrated into an existing complete code.
lst = list() #list declaration in Python
#if item is already on the list it will just continue and will not execute the else command
if yitem in lst: continue
else:
print(" Item 'yitem' is not present in the list, appending item to the list.")
lst.append(yitem) #action that will be done if item was not found on the list
For completeness sake if something needs to be done if the item is found, then the code will be like this:
if yitem in lst:
print "Item is found and is present on the list."
#do something more if required
else:
print(" Item 'yitem' is not present in the list, appending item to the list.")
lst.append(yitem) #action that will be done if item was not found on the list
Cheers! Till next time...enjoy Python programming and stay away from the real Python it's not forgiving.
================================
Free Android Apps:
Click links below to find out more:
Excel Keyboard guide:
Heaven's Dew Fall Prayer app for Android :
Catholic Rosary Guide for Android:
Pray the Rosary every day, countless blessings will be showered upon your life if you recite the Rosary faithfully.
https://play.google.com/store/apps/details?id=com.myrosaryapp
https://play.google.com/store/apps/details?id=com.myrosaryapp
Comments
Post a Comment