Simpler way to achieve this is using nested list comprehension :
>>> col = ['cat','dog','bird','fish']
>>> col_names= ['cat', 'bird']
>>> [[c for c in col if c !=cn] for cn in col_names]
[['dog', 'bird', 'fish'], ['cat', 'dog', 'fish']]
The code you shared is not logically correct. If you want to do it with explicit for
loop, you can write it like this:
new_list = []
for cn in col_names:
temp_list = []
for c in col:
if c != cn:
temp_list.append(c)
new_list.append(temp_list)
print(new_list)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…