0

I am writing code to sum the second index in a list within a list:

employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]

i.e. I want to do 5+3+3= 11

for i in range(len(employees)):
    total_importance = sum(input[i][1])
    or 
    total_important = sum(map(list.append, input[i][1]))

    print(total_importance)

Now both of the options above for total_importance produce an error- int object is not itterable or other errors. Is there any way I can amend the code above for it to work?

I have another method where I simply append input[i][1] into a new list and then sum from there, and this easily works. But for my knowledge I want to ask if there is any way to amend either of the code above? Thanks

  • To start with, I suggest, instead of doing an addition, you just print out those numbers. You can use the `for` you have and the print statement. You just need to find the syntax to access the numbers you are after. – quamrana Apr 19 '21 at 08:26

5 Answers5

2

You can use list comprehension to extract the second element and sum on that new list:

sum([l[1] for l in employees])

The above code returns 11 on your employees object.

This also works without creating a new list, by passing a generator to the sum function:

sum(l[1] for l in employees)
Marci
  • 76
  • 2
1

You might consider to reduce the list to the desired sum

reduce((lambda x, y: x+y[1]), employees, 0)

EDIT: Please note that input is a built in function in python. In the above mentioned reduce method x represents the "running sum" of summed up elements and y represents an item from the employees list. So x is an int value and y is a list. 0 is the initial value for the calculation:

>>> import functools
>>> employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]
>>> functools.reduce((lambda x, y: x+y[1]), employees, 0)
11
Matthias
  • 11
  • 4
1

You can take advantage of pythons powerful unpack features:

sum(second for first,second,*rest in employees)

This will unpack into three variables, first, second and the rest (in case of uneven lengths).

If you only care to use the second you can use:

sum(second for _,second,*_ in employees)
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
1

The line where you wrote total_importance = sum(input[i][1]), inside the sum() you are basically extracting one integer from that list and using sum() on it. This is equivalent to doing sum(2) which would of course cause an error.

Instead, you can do

total_importance = 0
for i in range(len(employees)):
   num = employees[i][1]
   total_importance += num
Remor
  • 13
  • 5
0

So, this is because you are using sum function which works on list. But you are selecting individual element. So you need to do:

total_importance += employees[i][1]

You can make this code more general like even if the second element is list, it will work.

# employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]
employees = [[1, [5,6], [2, 3]], [2, 3, []], [3, 3, []]]
total_importance=0
for i in range(len(employees)):
    if isinstance(employees[i][1], list):
        total_importance += sum(employees[i][1])
    else:
        total_importance += employees[i][1]

print(total_importance)