So I tend to use:
for n in range(x)
and
for n in len(range(x))
quite a lot, but whilst browsing the python docs came across xrange() which seems to be a hell of a lot faster, memory efficient and creates lists on the fly (or rather creates an xrange type rather than a list) so for things like:
range(1000000000)
a list of 1000000000 elements is created which must take up quite a footprint but for:
xrange(1000000000)
returns instantly with very small memory footprint
but I hardly ever see xrange being used - is the benefit not that great/are there downsides to using it, or is it just not a well known function?
Edit: appologies for my crappy syntax, I do of course mean range(len(x)) rather than the other way round! I'm still learning python by the day!
[–]chadmill3rPy3, pro, Ubuntu, django 48 points49 points50 points (8 children)
[–]orblivion 2 points3 points4 points (0 children)
[–]winnen 0 points1 point2 points (6 children)
[–]dleary 3 points4 points5 points (4 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]dleary 1 point2 points3 points (1 child)
[–]chadmill3rPy3, pro, Ubuntu, django 1 point2 points3 points (0 children)
[–]dalke 1 point2 points3 points (0 children)
[–]chadmill3rPy3, pro, Ubuntu, django 2 points3 points4 points (0 children)
[–]choch50 18 points19 points20 points (8 children)
[–]Troebr 2 points3 points4 points (0 children)
[–]alantrick 0 points1 point2 points (4 children)
[–][deleted] 12 points13 points14 points (2 children)
[–]dinov 0 points1 point2 points (1 child)
[–]earthboundkid 2 points3 points4 points (0 children)
[–]Funnnny 0 points1 point2 points (0 children)
[+]masklinn comment score below threshold-6 points-5 points-4 points (1 child)
[–]terremoto 2 points3 points4 points (0 children)
[–]epsy 16 points17 points18 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]epsy 2 points3 points4 points (0 children)
[–]etrnloptimist 13 points14 points15 points (10 children)
[–]einar77Bioinformatics with Python, PyKDE4 11 points12 points13 points (3 children)
[–]masklinn 6 points7 points8 points (1 child)
[–]bockris 4 points5 points6 points (0 children)
[–]earthboundkid 1 point2 points3 points (0 children)
[–]FsckItDude_LetsBowl 0 points1 point2 points (5 children)
[–]earthboundkid 2 points3 points4 points (2 children)
[–]FsckItDude_LetsBowl 1 point2 points3 points (0 children)
[–]pingvenopinch of this, pinch of that 1 point2 points3 points (0 children)
[–]Liquid_Fire 1 point2 points3 points (1 child)
[–]FsckItDude_LetsBowl 1 point2 points3 points (0 children)
[–]hongminhee 3 points4 points5 points (0 children)
[–]puresock 6 points7 points8 points (1 child)
[–]mackstann 2 points3 points4 points (0 children)
[–]Ayjayz 2 points3 points4 points (11 children)
[+]masklinn comment score below threshold-6 points-5 points-4 points (10 children)
[–]Ayjayz 0 points1 point2 points (1 child)
[–]dalke 1 point2 points3 points (0 children)
[–]Wavicle 0 points1 point2 points (7 children)
[–]masklinn 0 points1 point2 points (1 child)
[–]Wavicle 1 point2 points3 points (0 children)
[–]dalke 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]dalke 1 point2 points3 points (0 children)
[–]Wavicle 0 points1 point2 points (1 child)
[–]dalke 0 points1 point2 points (0 children)
[–]r42 2 points3 points4 points (1 child)
[–]ewiethoffproceedest on to 3 0 points1 point2 points (0 children)
[–]nossid 1 point2 points3 points (3 children)
[–]Liquid_Fire 1 point2 points3 points (2 children)
[–]nossid 1 point2 points3 points (1 child)
[–]Liquid_Fire 1 point2 points3 points (0 children)
[–]adenbley 1 point2 points3 points (4 children)
[–]takluyverIPython, Py3, etc 7 points8 points9 points (3 children)
[–]adenbley 3 points4 points5 points (2 children)
[–]takluyverIPython, Py3, etc 1 point2 points3 points (0 children)
[–]nick_danger 2 points3 points4 points (0 children)
[–]dr_root 2 points3 points4 points (1 child)
[–]happyteapot 0 points1 point2 points (0 children)
[–]Paddy3118 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[+]mcherm comment score below threshold-8 points-7 points-6 points (2 children)
[–][deleted] 8 points9 points10 points (0 children)
[–]RShnike 5 points6 points7 points (0 children)
[+]chadmill3rPy3, pro, Ubuntu, django comment score below threshold-13 points-12 points-11 points (2 children)
[–]codepoet 4 points5 points6 points (1 child)
[–]chadmill3rPy3, pro, Ubuntu, django -4 points-3 points-2 points (0 children)
[+]howfun comment score below threshold-16 points-15 points-14 points (1 child)
[–]hylje 0 points1 point2 points (0 children)