Those 2 functions purpose is very similar when you use them in python, but the major different is range() function will generate the new list but xrange() is not. So, if your purpose doesn't use the advantage of the list to access you can just use xrange() function to make your code use less resources. For example, for-loop statement you can just use xrange() function to enumerate your loop.
for x in xrange(5):
print x
Even you change xrange() function to range() function the result will be the same as:
0
1
2
3
4
But, As I already said, behind the scene range() function will generate it as a list that may make it slow when generate the big list for normal enumerate, xrange() function is enough.
No comments:
Post a Comment