# ===================================================================
# Use del to remove an element by index, pop() to remove it by
# index if you need the returned value, and remove() to delete
# an element by value. The latter requires searching the list,
# and raises ValueError if no such value occurs in the list.
# ===================================================================
lst = [1,2,3,None,'a','b','c',[10,20,20,'a']]
print('--- display list ------------------------------------')
print(lst)
# -- display elements in the list
print('--- display list elements ---------------------------')
for x in lst:
print('{} {}'.format(x,type(x)))
print('--- test for element type ---------------------------')
for x in lst:
if isinstance(x,str):
print('found element type str')
# --- remove removes the first matching value, not a specific index
print("--- remove 'a' from list ----------------------------")
lst.remove('a')
print(lst)
print("--- remove 'a' from list again ----------------------")
try:
lst.remove('a')
except Exception as e:
print('ERROR: {}'.format(e))
print(lst)
# --- del removes a specific index
print('--- del element using index 2 -----------------------')
del lst[2]
print(lst)
# --- pop returns the removed element
print('--- pop [-1] return removed element ----------------')
x = lst.pop(-1)
print(x)
print(lst)