So, I just realized that I re-implemented two built-in Python functions on a small project I'm working on for ETSZONE. I just didn't know that these existed, so I'm writing about them here in case you've overlooked them too.
sorted is useful if
you want to sort a copy of a list. Use
sorted() instead of copying the list
and then using
This was my re-implementation (and I think I still like its name better):
def sort(seq, **args): x = list(seq) x.sort(**args) return x
sorted function has been available since Python v2.4.
useful when you want a foreach loop, but you also need a loop counter around.
enumerate() instead of keeping a counter elsewhere.
For example, I was writing out a spreadsheet with
ooolib-python. For each spreadsheet cell to
write, I had to specify row and column indexes. I could write more natural loops
enumerate, while still having a counter to use as a row or column index.
This was my re-implementation (and its name would have never caught on):
def indexiter(iterable): return zip(range(len(iterable)), iterable)
enumerate function has been available since Python v2.3. Read about the
start parameter in the
This shows that it's a good idea to occasionally browse back through the very basic support a language gives you, since you might just find a couple useful tools in there that you had overlooked. If you're into Python, start here.