1

Just say I have an integer in Python, ex: 100.

I'd like to convert this integer into a list counting up to this integer, ex: [1,2,3,... 100]

What is the most efficient way to do this?

(In case you're wondering why I want to do this, it's because the Django Paginate library I want to use needs a list -- if there's a more elegant way to use Paginate without doing this, please let me know).

jac
  • 9,666
  • 2
  • 34
  • 63
user1328021
  • 9,110
  • 14
  • 47
  • 78

3 Answers3

7

The Python range() built-in does exactly what you want. E.g:

range(1, 101)

Note that range() counts from 0, as is normal in Computer Science, so we need to pass 1, and it counts until the given value, not including - hence 101 here to count to 100. So generically, you want range(1, n+1) to count from 1 to n.

Also note that in 3.x, this produces an iterator, not a list, so if you need a list, then you can simply wrap list() around your call to range(). That said - most of the time it's possible to use an iterator rather than a list (which has large advantages, as iterators can be computed lazily), in that case, there is no need to make a list (in 2.x, functionality like that of range in 3.x can be achieved with xrange().

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
3

The solution given by Lattyware is correct for generating the list, but if your using Django pagination you can just use paginator_instance.page_range

https://docs.djangoproject.com/en/dev/topics/pagination/#django.core.paginator.Paginator.page_range

DanielB
  • 2,798
  • 1
  • 19
  • 29
1

You can use range or xrange. xrange is faster, but returns an iterator.

>>>range(1,101)
[1, 2, 3, ... , 99, 100]

>>> xrange(1,101)
xrange(1, 101)

>>>list(xrange(1,101))
[1, 2, 3, ... , 99, 100]
Daniel
  • 19,179
  • 7
  • 60
  • 74
  • Why use `xrange()` just to make it a list again? – Gareth Latty Jan 06 '13 at 02:30
  • Just showing as an example. Maybe a better example would have been "for a in xrange(1,101)". – Daniel Jan 06 '13 at 02:31
  • @Lattyware on my machine at least, using the `timeit` module, `list(xrange(1,101))` is faster than `list(range(1,101))` – Fredrick Brennan Jan 06 '13 at 02:33
  • I would hope it would be! I was trying to give additional options. Although im not sure of the point of list(range(1,101)). – Daniel Jan 06 '13 at 02:34
  • 1
    @frb Yes, but that comparison makes no sense. The right comparison is `list(xrange(...)` to `range(...)`. – Gareth Latty Jan 06 '13 at 02:36
  • Oh, you're right. I've been in front of the computer too long today I think :) – Fredrick Brennan Jan 06 '13 at 02:38
  • 1
    @Ophion -- the point of `list(range(1,101))` would be python2.x and python3.x compatability in the case where you actually *need* a list. – mgilson Jan 06 '13 at 02:39
  • @mgilson That doesn't really make much sense, as `2to3` will convert calls to `range(...)` to `list(range(...))` in 3.x. – Gareth Latty Jan 06 '13 at 02:42
  • @Lattyware It's sometimes useful to write programs that run in both Python2.x and Python3.x interpreters. – Fredrick Brennan Jan 06 '13 at 02:43
  • @frb That's a pretty limited audience - and I think if one is going to do that, one has to throw trying to get optimal performance out of the window, which was the initial comparison. – Gareth Latty Jan 06 '13 at 02:45
  • 1
    @Lattyware -- Yeah, I know. But I've had other things which `2to3` didn't actually convert properly. Maybe it's being stubborn or stupid, but wherever possible, and as long as it's not going to change the performance of my program much, I try to support python2 and python3 in the same source file without relying on external tools. (usually it's not hard). – mgilson Jan 06 '13 at 02:56