Suppose I have a the following list
List = ['0000', '0010', '0020', '0030', '0040', '0050', '0060', '0070']
for a in List:
if [int(a[-2]) for a in List] % 2 != 0:
List.remove(a)
Expected Output
List = ['0000', '0020', '0040', '0060']
Suppose I have a the following list
List = ['0000', '0010', '0020', '0030', '0040', '0050', '0060', '0070']
for a in List:
if [int(a[-2]) for a in List] % 2 != 0:
List.remove(a)
Expected Output
List = ['0000', '0020', '0040', '0060']
You are removing items from list while iterating, maybe that's the root of the problem, Try out a list comprehension
List = ['0000', '0010', '0020', '0030', '0040', '0050', '0060', '0070']
List = [x for x in List if not x[-2] in [str(z) for z in range(1,10,2)]]
print(List)
[6,7,8,9] am I correct to write [str(z) for z in range(6,8,1)]?
li.remove(a)What isli? You haven't defined that variable.Listbut an answer has been supplied already. Thank youTypeError: unsupported operand type(s) for %: 'list' and 'int'since you are trying to apply%to something between brackets!