What is the difference between `range` and `xrange`

The difference between range and xrange in Python is primarily related to memory efficiency and the type of object they return. range returns a list, while xrange returns an xrange object, which generates the numbers on the fly (lazily) and is thus more memory efficient, especially for large ranges.

However, it's important to note that xrange was removed in Python 3, so if you're using Python 3, you only have range available, which behaves like xrange in Python 2 by generating numbers on demand.

Keywords: range, xrange, Python 2, Python 3, memory efficiency, list, xrange object
Description: Understanding the differences between range and xrange in Python helps in writing more efficient code and leveraging the proper iterator for your needs.
# Example of range in Python 3 for i in range(1, 10): print(i)

Keywords: range xrange Python 2 Python 3 memory efficiency list xrange object