·
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!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.