S
Steven D'Aprano
If I want to iterate over part of the list, the normal Python idiom is to
do something like this:
alist = range(50)
# first item is special
x = alist[0]
# iterate over the rest of the list
for item in alist[1:]
x = item
The important thing to notice is that alist[1:] makes a copy. What if the
list has millions of items and duplicating it is expensive? What do people
do in that case?
Are there better or more Pythonic alternatives to this obvious C-like
idiom?
for i in range(1, len(alist)):
x = alist
do something like this:
alist = range(50)
# first item is special
x = alist[0]
# iterate over the rest of the list
for item in alist[1:]
x = item
The important thing to notice is that alist[1:] makes a copy. What if the
list has millions of items and duplicating it is expensive? What do people
do in that case?
Are there better or more Pythonic alternatives to this obvious C-like
idiom?
for i in range(1, len(alist)):
x = alist