Skip to content

Tag: enumerate

How to accept input from an enumerated list in Python?

So I have a list that I want to number using enumerate(), then have the user choose an item from the list using the corresponding number. Is there a way to do this? (This is broken code but hopefully, you get an idea of what I want to do) Answer It’s always a good idea to validate user input –

Python: Enumeration based on an enumerated list

I have several vertexes that compose two triangles. Vertexes: Triangles: Now I need to export this data to a .txt file, to have the following output composed by two different parts: The first part was easy, as I only needed to enumerate each vertex based on the aforementioned list. The problem is with the sec…

How can I limit iterations of a loop?

Say I have a list of items, and I want to iterate over the first few of it: Naive implementation The Python naïf coming from other languages would probably write this perfectly serviceable and performant (if unidiomatic) code: More idiomatic implementation But Python has enumerate, which subsumes about half o…

Python’s enumerate in Ruby?

Is something built in for this? It doesn’t need to have it’s members immutable, it just needs to be in the standard library. I don’t want to be the guy who subclasses the Array class to add a Python feature to a project. Does it have a different name in Ruby? Answer Something like this in Py…

Is enumerate in python lazy?

I’d like to know what happens when I pass the result of a generator function to python’s enumerate(). Example: Is the enumeration iterated lazily, or does it slurp everything into the <enumerate object> first? I’m 99.999% sure it’s lazy, so can I treat it exactly the same as the …