Python range to list

前端 未结 4 1923
时光取名叫无心
时光取名叫无心 2020-12-16 18:52

I am trying to convert a range to list.

nums = []
for x in range (9000, 9004):
    nums.append(x)
    print nums

output

[90         


        
4条回答
  •  再見小時候
    2020-12-16 19:35

    Python 3

    For efficiency reasons, Python no longer creates a list when you use range. The new range is like xrange from Python 2.7. It creates an iterable range object that you can loop over or access using [index].

    If we combine this with the positional-expansion operator *, we can easily generate lists despite the new implementation.

    [*range(9000,9004)]
    

    Python 2

    In Python 2, range does create a list... so:

    range(9000,9004)
    

提交回复
热议问题