• Register
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?
Thanks for your help, mate.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

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

13 9 6
94,260 points

Related questions

0 votes
1 answer 2 views
2 views
Problem: find an item in a list python.
asked 5 days ago Dan phillip 4.8k points
0 votes
1 answer 1 view
1 view
Problem: Please help me with my query as to how to replace an item in a list python?
asked Apr 2 Sana8989 8.9k points
0 votes
1 answer 10 views
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?
asked Feb 27 sasha 16.2k points
0 votes
1 answer 6 views
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!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 6 views
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!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 4 views
4 views
Problem: Can anybody solve this : how to print last item in list python?
asked Mar 30 Madhav96 2.3k points
0 votes
1 answer 5 views
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!
asked Mar 6 Mashhoodch 13k points
0 votes
1 answer 6 views
6 views
Problem: Why I got the error : check if item is in list python HELP! I need help understanding this error: check if item is in list python Any ideas on what I need to do to fix this? Thanks!
asked Feb 28 Mashhoodch 13k points
0 votes
1 answer 13 views
13 views
Problem: Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python? For instance, in this snippet, l.index(999_999, 999_990, 1_000_000) is roughly five orders of magnitude faster than straight l.index(999_999), because the former only has to search 10 entries, while the latter searches a million:
asked Feb 20 Mashhoodch 13k points
0 votes
1 answer 8 views
8 views
Problem: Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python? The index() method returns the index of the specified element in the list. The syntax of the list index() method is: list.index(element, start, end)
asked Jan 26 Mashhoodch 13k points