Search code examples
pythonlistpython-2to3

2to3 range(...) -> list(range(...))


I know python 2to3 changes all xrange to range and I don't see any problem with this. My question is about how it changes range(...) into list(range(...)): is it dumb and just blindly search and replaces them all, or is it smart enough to tell when the list isn't needed and leaving it as range would be better?


Solution

  • I don't know how intelligent it really is, but it certainly doesn't add list() to every range().

    For example, the following:

    print(range(10))
    

    is changed to:

    print(list(range(10)))
    

    However, the following:

    for el in range(10):
      print(el)
    

    is left untouched.

    This clearly indicates that it's more sophisticated than a blind search-and-replace.