1

There are the map,reduce,filter functions to make list comprehensions.

What is the difference between passing a xrange argument or a range argument to each of these functions?
For example:

map(someFunc, range(1,11))

OR

map(someFunc,xrange(1,11))
5
  • Ask separate questions rather than vaguely related questions. You will get better answers. Commented Nov 24, 2012 at 1:54
  • @WaleedKhan OK, I'll remove the second question from here :) Commented Nov 24, 2012 at 1:54
  • The answers to these questions depend on the version of python you're using, FYI. Commented Nov 24, 2012 at 1:55
  • @JoelCornett Oh, I didn't know that. Well, I'm using python 2.7. Commented Nov 24, 2012 at 1:55
  • List comprehensions are an (often better) way of producing a list with similar functionality to map() and filter() but with a syntax which is usually significantly more readable. Commented Nov 24, 2012 at 2:19

3 Answers 3

4

In Python 2, range returns an actual list, while xrange returns a generating function which can be iterated over. Since map only cares that it iterates over a sequence, both are applicable, though xrange uses less memory. Python 3's xrange replaces range, so your only option is to use range to return a generating function. (You can use [i for i in range(10)] to generate the actual range if so desired.)

The modulus operator returns 0, a falsey value, for the even numbers: 2 mod 2 is 0. As such, the even numbers are filtered out.

Sign up to request clarification or add additional context in comments.

4 Comments

Then there will be no difference whether I call these functions with xrange or with range?
The end result will be the same, but xrange will do it with less resources.
And as you said, in python 3 the range function becomes the xrange? So in fact, there is no python 2.7 range in python 3? (function that creates a list).
@Lior No, but you can use a list comprehension as stated. You can also use list(range(10)) for the same effect.
2

Actually, the map, reduce, and filter functions are an alternative to list comprehensions. The term "list comprehension" refers to the specific syntactic construct; anything that doesn't look like a list comprehension is necessarily not a list comprehension.

They actually predate list comprehensions, and are borrowed from other languages. But most of those languages have ways of constructing anonymous functions which are more powerful than Python's lambda, so functions such as these are more natural. List comprehensions are considered a more natural fit to Python.

The difference between range and xrange is that range actually constructs a list containing the numbers that form the range, whereas an xrange is an object that knows its endpoints and can iterate over itself without ever actually constructing the full list of values in memory. xrange(1,1000) takes up no more space than xrange(1,5), whereas range(1,1000) generates a 999-element list.

Comments

2

If range() and xrange() were implemented in the Python language, they would look something like this:

def xrange(start, stop=None, step=1):
    if stop is None: stop, start = start, 0
    i = start
    while i < stop:
        yield i
        i += step

def range(start, stop=None, step=1):
    if stop is None: stop, start = start, 0
    acc, i = [], start
    while i < stop:
        acc.append(i)
        i += step
    return acc

As you can see, range() creates a list and returns it, while xrange() lazily generates the values in a range on demand. This has the advantage that the overhead for creating a list is avoided in xrange(), since it doesn't store the values or create a list object. For most instances, there is no difference in the end result.

One obvious difference is that xrange() doesn't support slicing:

>>> range(10)[2:5]
[2, 3, 4]
>>> xrange(10)[2:5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence index must be integer, not 'slice'
>>> 

It does, however support indexing:

>>> xrange(11)[10]
10

1 Comment

@Lior: No problem. I realized that it came a bit late (Waleed's answer is great), but I had already almost finished writing my answer at that point :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.