The difference between range and xrange in Python

Source: Internet
Author: User

Let's take a look at the usage of range and xrange

Help (Range)
Help on built-in function range in module __builtin__:

Range (...)
Range (stop), List of integers
Range (start, stop[, Step), List of integers

Return a list containing an arithmetic progression of integers.
Range (I, j) returns [I, I+1, i+2, ..., j-1]; Start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range (4) returns [0, 1, 2, 3]. The end point is omitted!
These is exactly the valid indices for a list of 4 elements.

Example description
eg
In [4]: Range (6)
OUT[4]: [0, 1, 2, 3, 4, 5]

In [5]: Range (1,6)
OUT[5]: [1, 2, 3, 4, 5]

In [6]: Range (1,6,1)
OUT[6]: [1, 2, 3, 4, 5]

In [7]: Range (1,6,2)
OUT[7]: [1, 3, 5]

Range () Returns a list object (listing) with a default step of 1, which outputs all content at once


Help on the class xrange in module __builtin__:
Class Xrange (Object)
| Xrange (stop), Xrange object
| Xrange (Start, stop[, Step), Xrange object
|
| Like range (), but instead of returning a list, returns an object that
| Generates the numbers in the range on demand. For looping
| Slightly faster than range () and more memory efficient.
|
| Methods defined here:
|
| __getattribute__ (...)
| x.__getattribute__ (' name ') <==> x.name
|
| __getitem__ (...)
| x.__getitem__ (y) <==> X[y]
|
| __iter__ (...)
| X.__ITER__ () <==> iter (x)
|
| __len__ (...)
| x.__len__ () <==> len (x)
|
| __reduce__ (...)
|
| __repr__ (...)
| x.__repr__ () <==> repr (x)
|
| __reversed__ (...)
| Returns a reverse iterator.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__NEW__ (S, ...)-A new object with type S, a subtype of T

In []: xrange (5)
OUT[15]: xrange (5)

in [+]: List (xrange (5))
OUT[16]: [0, 1, 2, 3, 4]

in [+]: List (xrange (1,5))
OUT[17]: [1, 2, 3, 4]

in [+]: List (xrange (0,6,2))
OUT[18]: [0, 2, 4]

Range () and xrange () are used for looping
in [+]: For I in Range (10,20):
.....: Print I
....:
10
11
12
13
14
15
16
17
18
19

in [+]: for I1 in Xrange (10,20):
....: Print I1
....:
10
11
12
13
14
15
16
17
18
19


The results of both outputs are the same, in fact there are many differences, and range directly generates a list object, xrange directly generates a generator:

The above example knows: to generate a very large sequence of numbers, using xrange will be much better than range performance, because there is no need to open a large amount of memory in the first place

So the performance of the xrange cycle is better than range, especially when the return is very large. Try to use xrange, unless you are going to return a list.

The difference between range and xrange in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.