How to turn all numbers in a list into their negative counterparts?

前端 未结 5 597
别跟我提以往
别跟我提以往 2020-12-10 13:48

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] into

5条回答
  •  旧时难觅i
    2020-12-10 14:20

    If you want to modify a list in place:

    mylist = [ 1, 2, 3, -7]
    print(mylist)
    for i in range(len(mylist)):
        mylist[i] = -mylist[i]
    print(mylist)
    

提交回复
热议问题