Python Forum

Full Version: Accessing first list element when at end of loop!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI,
I'm new to this so sorry if my formatting is a bit off!
Essentially I wish to have a list, say [1,2,3,4] and to loop through it, and sum the previous, current and next element.
When at the first element, in this case 1, i wish for the previous element to be the last in the list (4), so the sum is 4+1+2. I would also like to do this for the final element, so the sum would be 3+4+1. Is there a cleaner way to do this than a few if statements?

many thanks,

JY Smile Smile Smile
In order to get advice you should post your code.