I tried to add only using the pop method without modules

a = [-1, 2, -1, 3, 3, -5] b = a.pop(2) c = a.pop(3) d = b + c print(d) 
  • one
    What does "counting integers" mean? In your list all the numbers are integers. Amount through sum(a) does not fit? - insolor
  • I want to add numbers from 1 element to the end using a loop is it possible? - user310802 5:27 pm
  • and I need to add their modules - user310802
  • five
    Learn to formulate the exact condition of the problem - MBo
  • 2
    sum(abs(x) for x in a) - andreymal

3 answers 3

As far as I understood the task:

 a = [-1, 2, -1, 3, 3, -5] b = 0 for digit in a: b += abs(digit) print(b) 

If you do not fool with the cycle then:

 a = [-1, 2, -1, 3, 3, -5] b = sum(abs(i) for i in a) print(b) 

Or so:

 a = [-1, 2, -1, 3, 3, -5] b = sum(map(abs, a)) print(b) 
  • 2
    Or this: b = sum([abs(i) for i in a]) -> b = sum(abs(i) for i in a) - gil9red
  • And this is not a duplication of the answer, but a hint to you :) - gil9red
  • There are no square brackets. Is it not noticeable? - gil9red
  • Got it. I agree. Thank. I didn’t immediately realize that the arrow between the expressions))) - Andrey
 a = [-1, 2, -1, 3, 3, -5] acc = 0 for i in a: acc += abs(i) print(acc) 

or

 from functools import reduce from operator import add a = [-1, 2, -1, 3, 3, -5] b = reduce(add, map(abs, a)) print(b) 

    Option through pop :

     a = [-1, 2, -1, 3, 3, -5] b = 0 while a: b += abs(a.pop()) print(b) # 15