Logical indexing with lists

As Already explained you are setting the second element using the result of comparing to a list which returns True/1 as bool is a subclass of int. You have a list not a numpy array so you need to iterate over it if you want to change it which you can do with a list comprehension using if/else logic:

temp = [1,2,3,4,5,6]
temp[:] = [0 if ele != 1 else ele for ele in temp ]

Which will give you:

[1, 0, 0, 0, 0, 0]

Or using a generator expression:

temp[:] = (0 if ele != 1 else ele for ele in temp)

Leave a Comment