Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

Why or why not?

1 Answer

0 votes
by (106k points)

When you see performance then xrange() is preferred, especially when you're iterating over a large range, xrange() is usually better. Whereas, there are still a few cases why you might prefer range():

  • If you are a python 3 user, then you will be using range(), it does what xrange()does in Python versions less than 3.0, because xrange() does not exist in Python 3 or greater versions. In case you want to write code that will run on both Python 2 and Python 3, you can't use xrange().

  • The range() function can actually be faster in some cases - eg. if iterating over the same sequence multiple times. xrange() has to reconstruct the integer object every time, but the range() will have real integer objects. (It will always perform worse in terms of memory, however)

  • You can not use xrange() in all cases where a real list is needed. For instance, it doesn't support slices or any list methods.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers


108k users

Browse Categories