url[0,1,2,3,4,5,6,7]
is an incorrect syntax, as the error shows the index of a list must be an integer. 0,1,2,3,4,5,6,7
is just syntactic sugar to create a tuple, that is, the above equals url[(0,1,2,3,4,5,6,7)]
.
If you want to iterate over all the elements of a list except the first you have several options:
-
Slicing:
url = ['a.com', 1, 2, 3, 4, 5, 6, 7]
for item in url[1:]:
print(item)
Simple and good option for lists with few elements, but creates a copy of the object which is not very efficient for lists with many elements.
-
Use an iterator and consume the first element before for
. Possibly the best option for relatively long lists:
url = ['a.com', 1, 2, 3, 4, 5, 6, 7]
url_iter = iter(url)
next(url_iter)
for item in url_iter:
print(item)
-
Use itertools.islice
:
from itertools import islice
url = ['a.com', 1, 2, 3, 4, 5, 6, 7]
for item in islice(url, 1, None):
print(item)