Python enumerate list setting start index but without increasing end count -


i want loop through list counter starting @ 0 list starting index @ 1 eg:

valuelist = [1, 2, 3, 4] secondlist = ['a', 'b', 'c', 'd']  i, item in enumerate(valuelist, start=1):     print(secondlist[i]) 

the code fails index out of range error (i realize because ends @ length of list -1 plus start value , python lists 0 indexed using in way call ith element in list not valid). following works addition of test greater 0 looks un-pythonic.

valuelist = [1, 2, 3, 4] secondlist = ['a', 'b', 'c', 'd']  i, item in enumerate(valuelist, start=0):     if > 0:           print(secondlist[i])  

enumerate not right choice, there way?

it sounds if want slice list instead; still start enumerate() @ 1 same indices:

for i, item in enumerate(valuelist[1:], start=1): 

this loops on valuelist starting @ second element, matching indices:

>>> valuelist = [1, 2, 3, 4] >>> secondlist = ['a', 'b', 'c', 'd'] >>> i, item in enumerate(valuelist[1:], start=1): ...     print(secondlist[i]) ...  b c d 

in case, i'd use zip() instead, perhaps combined itertools.islice():

from itertools import islice  value, second in islice(zip(valuelist, secondlist), 1, none):     print(value, second) 

the islice() call skips first element you:

>>> itertools import islice >>> value, second in islice(zip(valuelist, secondlist), 1, none): ...     print(value, second) ...  2 b 3 c 4 d 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -