-2

Basically, I can't find a solution/syntax for the following problem: I want to loop/iterate through a list like this:

my_list = list(range(0,11)
for x in my_list:
    my_sum = sum(my_list[0:2])   # Sum is only an example, I actually want to do something else but that's not part of the question
    # Now move on to my_list[1] and do the sum of my_list[1] and my_sum and so on iteratively

I hope it's understandable what I want to do, I don't think I can explain it much better. I think it can be solved with a relatively easy while or for loop but I can't get the hang of it.

Basically, the characters influence the next one and so on

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    can you just give us an expected input and output? what do you want to do to *each* element of the list – Ironkey Oct 21 '20 at 14:40
  • probably just lacking indent on line 3 – Gulzar Oct 21 '20 at 14:40
  • Absolutely no idea what you are trying to achieve. You are not even using x, the iteration. If you want to sum on part of the list at each iteration, then have the indices [0, 2] change with variables depending on x, the element iterated on; if you want to keep the sum and add on it in-between iteration, then use a variable outside the loop.. Anything can be done, but your use case is absolutely unclear. – Mathieu Oct 21 '20 at 14:44
  • Does this answer yor question? [Iterate a list as pair (current, next) in Python](https://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python) – wwii Oct 21 '20 at 14:55
  • Or does this answer your question? [How to find the cumulative sum of numbers in a list?](https://stackoverflow.com/questions/15889131/how-to-find-the-cumulative-sum-of-numbers-in-a-list) – wwii Oct 21 '20 at 14:57

2 Answers2

2

You can zip the list with its own tail:

for t in zip(my_list, my_list[1:]):
    my_sum = sum(t)

To avoid creating a partial shallow copy of the list, use islice:

from itertools import islice


for t in zip(my_list, islice(my_list, 1, None)):
    my_sum = sum(t)

(A nice helper function would be

def itail(itr):
    return islice(itr, 1, None)


for t in zip(my_list, itail(my_list)):
    my_sum = sum(t)

)

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Yes, that's because the OP's question doesn't do anything with `my_sum` either. I am assuming there is more to the real loop. – chepner Oct 21 '20 at 14:59
  • The comment at the end of the loop looks like maybe a cumulative sum. – wwii Oct 21 '20 at 15:01
  • 2
    Yes, but I am unconcerned with what the OP does with `my_sum`. The question is how to iterate over the list using a sliding window. – chepner Oct 21 '20 at 15:10
  • Thank you very much foryour answers, I realised I didn't specify my problem enough and did a [new post](https://stackoverflow.com/questions/64469205/how-to-iterate-through-a-list-using-xor-in-python), if you still want to help. – WamboRambo Oct 21 '20 at 18:01
0

I think what you want to do is:

sum = 0
for i in range(len(my_list)):
    for j in range(i):
        sum += my_list[i]
  • Thank you very much foryour answers, I realised I didn't specify my problem enough and did a [new post](https://stackoverflow.com/questions/64469205/how-to-iterate-through-a-list-using-xor-in-python), if you still want to help. – WamboRambo Oct 21 '20 at 18:01