0

How do you switch a negative to a positive and a postive to a negative in a list?

I want to make a function to do this. For example [1,-3, 5] would become [-1, 3, -5]

martineau
  • 119,623
  • 25
  • 170
  • 301
Nat Kar
  • 31
  • 7

1 Answers1

2

Just use list comprehension:

a = [1, -3, 5]
negative = [-x for x in a]
print(negative)

Output:

[-1, 3, -5]
Lev Zakharov
  • 2,409
  • 1
  • 10
  • 24