If I have a list with say [7,6,5,4,3,2,1] how can I make it add upp every second number, for instance 7 + 5 + 3 + 1?
I've tried adding mylist[0] + mylist[2] etc but it is very tedious.
sum(mylist[::2])
the mylist[::2] takes every other item, and sum sums it.
If you want to have the first, third, fifth et cetera item, you can use:
sum(list[1::2])
This will first omit the first item (with the 1 part), then do the same as in the first command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With