9

I am trying to iterate through the range(750, 765) and add the non-sequential numbers 769, 770, 774. If I try adding the numbers after the range function, it returns the range list, then the individual numbers:


>>> for x in range(750, 765), 769, 770, 774: print x
... 
[750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764]
769
770
774

How can I get all the numbers in a single list?

adam
  • 6,582
  • 4
  • 29
  • 28

6 Answers6

23

Use the built-in + operator to append your non-sequential numbers to the range.

for x in range(750, 765) + [769, 770, 774]: print x
Jason Coon
  • 17,601
  • 10
  • 42
  • 50
  • 1
    -1. This is not a good answer, nor is it for any others. itertools.chain() should be used. – Devin Jeanpierre Mar 24 '09 at 17:37
  • 7
    for adding 3 numbers? that's hardly a performance hit. It will require more time to import itertools. If this is being done in a loop over thousands of ranges, then yes, maybe you should consider the chain method. – Jason Coon Mar 24 '09 at 17:45
  • The range builtin uses a generator and will not allocate all the numbers in memory unless necessary. By doing the concatenation your forcing it to be allocated. – mustpax Mar 24 '09 at 17:58
  • @multipax: No, it always creates a list in 2.x . In 3.0 it returns a non-list iterable, but it won't work like that. – Devin Jeanpierre Mar 24 '09 at 18:22
  • It's not just three numbers. This actually creates 15 numbers in memory, 3 numbers in memory, then copies the 15 and 3 into a new 18-sized block in memory. It's just not decent, and won't work well for larger numbers. And there's no cost to using itertools.chain(), so why do otherwise at all? – Devin Jeanpierre Mar 24 '09 at 18:23
  • +1 for being clear and concise and simply solving the stated problem – David Z Mar 24 '09 at 18:37
  • 3
    Premature optimization is premature. – Robert Rossney Mar 24 '09 at 20:12
  • Yes it is, but it's not harmful. Is there any downside to premature optimization when it boils down to using the same amount of effort, sacrificing no flexibility, and in fact actually gaining flexibility (noticed how people had to use list(range()) for 3.0?)? Dogmatic avoidance means little. – Devin Jeanpierre Mar 24 '09 at 20:23
18

There are two ways to do it.

>>> for x in range(5, 7) + [8, 9]: print x
...
5
6
8
9
>>> import itertools
>>> for x in itertools.chain(xrange(5, 7), [8, 9]): print x
...
5
6
8
9

itertools.chain() is by far superior, since it allows you to use arbitrary iterables, rather than just lists and lists. It's also more efficient, not requiring list copying. And it lets you use xrange, which you should when looping.

Devin Jeanpierre
  • 92,913
  • 4
  • 55
  • 79
  • 1
    I am not familiar with the itertools library. Thank you for suggesting it. I'll read up on it. – adam Mar 24 '09 at 17:39
11

The other answers on this page will serve you well. Just a quick note that in Python3.0, range is an iterator (like xrange was in Python2.x... xrange is gone in 3.0). If you try to do this in Python 3.0, be sure to create a list from the range iterator before doing the addition:

for x in list(range(750, 765)) + [769, 770, 774]: print(x)
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Jarret Hardie
  • 95,172
  • 10
  • 132
  • 126
2

are you looking for this:

mylist = range(750, 765)
mylist.extend([769, 770, 774])
Vasil
  • 36,468
  • 26
  • 90
  • 114
  • I thought about doing it this way, but I knew there had to be a better, more concise way. Good suggestion though. – adam Mar 24 '09 at 17:37
  • yes, the accepted answer is shorter. I thought you needed to store the list for later use. – Vasil Mar 24 '09 at 17:42
  • You are right, I do need to store it. List comprehension to the rescue! mylist = [x for x in range(750, 765) + [769, 770, 774]] – adam Mar 24 '09 at 17:47
  • @adam: The comprehension is actually redundant there. "mylist = range(750,765)+[769,770,774]" gives the same result without a needless extra iteration. In python3 it doesn't as range() isn't a list, but you can do the same with "mylist = list(range(750,765) + [769,770,774]" instead. – Brian Mar 24 '09 at 17:58
1

In python3, since you cannot add a list to a range, if for some reason, you do not wish to import itertool, you can also do the same thing manually:

for r in range(750, 765), [769, 770, 774]:
    for i in r: 
        print(i)

or

for i in [i for r in [range(750, 765), [769, 770, 774]] for i in r]: 
    print(i)
Camion
  • 1,264
  • 9
  • 22
0

In python3:

for r in (list(range(750, 765)) + [769, 770, 774]):
    print(r)
Rexcirus
  • 2,459
  • 3
  • 22
  • 42