# How to reeplace an item in a list python

1 vote
24 views

## Problem:

Hello Kodlogers,

Myself is a high school student. I’ve started learning Python programming recently. I am wanting to replace an item in a list python. Let’s say I have a list of integers like:

``my_list = [10, -15, 1, 8, 0, 9, -5, 13, -1, 5]``

and I want to replace every item either -1 or 1. Negatives values with -1, positive values with 1 and leave 0 untouched. Is it doable? If doable, how?

## 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

## Related questions

2 views
Problem: find an item in a list python.
1 view
Problem: Please help me with my query as to how to replace an item in a list python?
10 views
Problem: For a list ["foo","bar","baz"]and a list item "bar", what is the cleanest way to get its index (1) in python?
6 views
Problem: how to I Solve this: error: delete an item from a list python HELP! I need help understanding this: delete an item from a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
6 views
Problem: how to Solve this: error: term refers to an individual item in a list. HELP! I need help understanding this: term refers to an individual item in a list. Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
4 views
Problem: Can anybody solve this : how to print last item in list python?
5 views
Problem: How to I Solve this: return index of item in list python HELP! I need help understanding this: return index of item in list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!