Python - how to turn all numbers in a list into their negative copies
I am trying to turn a list of positive numbers into a list of negative numbers with the same value in python 3.3.3
For example, turning [1,2,3] to [-1, -2, -3]
thanks for the help
I have this code:
xamount=int(input("How much of x is there"))
integeramount=int(input("How much of the integer is there"))
a=1
lista=[]
while(a<=integeramount):
if(integeramount%a==0):
lista.extend([a])
a=a+1
listb=lista
print(listb)
[ -x for x in listb]
print(listb)
This prints two identical lists when I want it to be positive and one negative.
+3
source to share
4 answers
There is also this method:
Small note, this will only work if all numbers start out positive. It will not affect 0. If you have negative numbers that you do not want to change, you need to add an IF statement below.
if num < 0: continue
numbers = [1, 2, 3, 4 ,5]
for num in numbers:
numbers[num-1] = num - (2*num)
numbers
[-1, -2, -3, -4, -5]
0
source to share