I believe, the more important is to understand the requirement over cosmetics while looking around a solution for an individual requirement.
In my opinion, it doesn't cost too much until the data we are working on is huge, where we have to be selective in our approach rest for small dataset either approach will be fine as mentioned below..
There are good explanation in PEP 469, PEP 3106 and Views And Iterators Instead Of Lists
In Python 3, there is only one method named items(). It uses iterators so it is fast and allows traversing the dictionary while editing. Note that the method iteritems() was removed from Python 3.
One can have a look at Python3 Wiki Built-In_Changes to get more details on it.
arr = pandas.Series([1, 1, 1, 2, 2, 2, 3, 3])
$ for index, value in arr.items():
print(f"Index : {index}, Value : {value}")
Index : 0, Value : 1
Index : 1, Value : 1
Index : 2, Value : 1
Index : 3, Value : 2
Index : 4, Value : 2
Index : 5, Value : 2
Index : 6, Value : 3
Index : 7, Value : 3
$ for index, value in arr.iteritems():
print(f"Index : {index}, Value : {value}")
Index : 0, Value : 1
Index : 1, Value : 1
Index : 2, Value : 1
Index : 3, Value : 2
Index : 4, Value : 2
Index : 5, Value : 2
Index : 6, Value : 3
Index : 7, Value : 3
$ for _, value in arr.iteritems():
print(f"Index : {index}, Value : {value}")
Index : 7, Value : 1
Index : 7, Value : 1
Index : 7, Value : 1
Index : 7, Value : 2
Index : 7, Value : 2
Index : 7, Value : 2
Index : 7, Value : 3
Index : 7, Value : 3
$ for i, v in enumerate(arr):
print(f"Index : {i}, Value : {v}")
Index : 0, Value : 1
Index : 1, Value : 1
Index : 2, Value : 1
Index : 3, Value : 2
Index : 4, Value : 2
Index : 5, Value : 2
Index : 6, Value : 3
Index : 7, Value : 3
$ for value in arr:
print(value)
1
1
1
2
2
2
3
3
$ for value in arr.tolist():
print(value)
1
1
1
2
2
2
3
3
There is a good post about How to iterate over rows in a DataFrame in Pandas though it says df but it explains all about item()
, iteritems()
etc.
Another good discussion over SO items & iteritems.