The shorter than shortest infinite for comprehension
If numpy is *-imported then using this "feature" of numpy.r_
/numpy.c_
an infinte counter is as cheap as:
for[i]in r_:print(i)
This is significantly shorter than this pure Python trick and has the added benefit of actually creating a proper counter.
This can also be used for a cheaper enumerate
:
S="abcde"
for x,[i]in zip(S,r_):print(i,x)
If you can live with a "boxed" counter, i.e. [0],[1],[2],... rather than 0,1,2,... then you can save the brackets around i
.
The boxes can even be useful: For example, if we have set up a loop anyway, then we can as a byproduct create a reversed "range" of the same length as the looped over sequence S:
C=[]
for e,C[:0]in zip(S,r_):f(e)
If for some reason you need double boxes [[0]],[[1]],[[2]],... use c_
instead of r_
.
pylab
is justmatplotlib.pyplot
+numpy
in a deprecated common namespace. Thenumpy
part ofpylab
is trivial in the sense that their imports have the same number of bytes, so only plotting stuff could additionaly come frompylab
, but I suspect that's not what you had in mind with your question. \$\endgroup\$numpy
packages. For examplepylab.randint
is valid where numpy would requirenumpy.random.randint
. So for golfingpylab
should provide shorter code. \$\endgroup\$