Solution:
Yes! You can replace each and every element in a list as you are wanting. We’ll define a list with some positive and negative numbers alongside zero. Then we’ll compare the numbers with zero and replace them with -1 and 1 according to their nature and leave zeros as they are.
Please dig into the program below carefully
my_list = [10, -15, 1, 8, 0, 9, -5, 13, -1, 5]
list1 = []
for item in my_list:
if item > 0:
list1.append(1)
elif item < 0:
list1.append(-1)
else:
list1.append(0)
print(my_list)
print(list1)
and the program will produce an output like this:
[10, -15, 1, 8, 0, 9, -5, 13, -1, 5]
[1, -1, 1, 1, 0, 1, -1, 1, -1, 1]
Thanks