[Tutor] Working with lists

Alan Gauld alan.gauld at btinternet.com
Sat Dec 13 19:19:00 CET 2008


<btkuhn at email.unc.edu> wrote

> Ideally, I'd be able to write:
>
> for num in list1:
> newlist.append(num+nextnum)
>
> This doesn't work, though because there's no way to access "nextnum" 
> unless I implement a "count" variable like this:

Or use an iterator:

>>> L = [1,2,3,4,5,6]
>>> i = iter(L)
>>> L2 = [n+i.next() for n in i]
>>> L2
[3, 7, 11]

Is that what you want?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list