3 ms·
There is a builtin function called `reversed`. You'd better remember that than the "useful" [::-1] idiom. The recommendation on `iteritems` had better be gene
by euccastro 14y ago
There is a builtin function called `reversed`. You'd better remember that than the "useful" [::-1] idiom.
The recommendation on `iteritems` had better be generalized to include `iterkeys`, `itervalues`, and other opportunities for using iterators rather than building lists. A note that the 'iter...' versions are removed in Python 3 (because iterator behaviour becomes the default) would be appropriate here.
In relation to collections, itertools is a great module to get familiarized with. I import * from this module. I consider functions there as if they were builtins.
"Conditional assignment" is a weak and misleading name. "Conditional expressions" is more descriptive. There is no assignment in
print "yes" if some_condition else "no"
as the article acknowledges later.
Using Ellipsis for getting all items is a violation of the Only One Way To Do It principle. The standard notation is [:].
I commend the good intentions of the writer, but I'm surprised that this article got 144 upvotes in HN.
- heretohelp 14y ago>There is a builtin function called `reversed`. You'd better remember that than the "useful" [::-1] idiom. Reversed produces an iterator and not a copy with all the dangers of mutable semantics. The [::-1] syntax is not equivalent as it returns a copy of the list as if it were reversed, changing up somewhat what it's doing. You'd better remember that you should know what you're talking about before you start dictating to other people how they should write their code.
- jongraehl 14y agolist(reversed(xs)) to make a copy rather than a view, though