· python

Python: Forgetting to use enumerate

Earlier this evening I found myself writing the equivalent of the following Python code while building a stop list for a topic model...


words = ["mark", "neo4j", "michael"]
word_position = 0
for word in words:
   print word_position, word
   word_position +=1

...which is very foolish given that there's already a function that makes it really easy to grab the position of an item in a list:


for word_position, word in enumerate(words):
   print word_position, word

Python does make things extremely easy at times - you're welcome future Mark!

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket