I'm following a data structures and algorithms course taught in Python at the moment and the instructor always opts to unpack arrays (well, lists) using a range functions (method 2 below) instead of the kind of upacking I'm used to seeing in python. I'm surprised as in my opinion method 1 is more readable.
Is there something I am missing that makes method 2 preferable? I thought maybe speed but another post on here mentions that calling range is actually slower due to having to 'generate' a new number each loop. Other thing I was thinking is that the instructor is just trying to make the code more language agnostic to something that can be replicated in other languages more easily.
Code sample:
Given a hash table index location:
index_location = [[ 'a' , 1 ], [ 'b' , 2 ], [ 'c' , 3 ], [ 'd' , 4 ]]
Method 1:
for key, value in index_location:
print(key)
Method 2:
for i in range(len(index_location)):
print(index_location[i][0])

I don't agree with the comment that this is just an opinion question since it can be tested. I do agree with the comments that boil down to "it depends on what you're trying to do." If your task only needs to do something for
len(your list)times, then Method 2 is faster by almost 2x (you can test this by just replacing thetmp_lst.appendbelow withpassin each test loop. However, if you need to access the elements in your list (which you do in your example), then Method 1 came out to be just a touch faster. This also has the benefit of being "more Pythonic" as in the other comments.