Scripting: "Can't Convert 'x' to double"

range() and xrange() are very similar. The difference is in how they set themselves up. We’ll use range() in our examples for clarity.

range() creates a list.
range(5) – a list of integers up to, but not including 5. If no start value is specified, it assumes zero.
range(2,5) – a list of integers starting at 2
range(0,5,2) – list of integers, staring at 0, with a step of 2
range(1,5,2) – list of integers, staring at 1, with a step of 2

If you need something with a step value, you must use a start value.

xrange() works the same way, but it doesn’t create a list. It returns a value ‘on demand’.

When to use one over the other? Python purists will say that for large ranges, xrange() is faster because there’s no overhead. It uses the same amount of memory, regardless of the range it represents. On the other hand, the list generated by range() can be used over and over again, sliced, diced, and modified to your heart’s content.

It should be noted that in Python 3, range() works like xrange(), and xrange() went away. It’ll be a while before Jython (and then Ignition) catches up to this, so hopefully we’ll retired before that happens. :wink: