tags:

views:

367

answers:

4

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?

+14  A: 

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
jcoon
-1. This is not a good answer, nor is it for any others. itertools.chain() should be used.
Devin Jeanpierre
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.
jcoon
+1: Simple list concatenation.
S.Lott
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
@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
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
+1 for being clear and concise and simply solving the stated problem
David Zaslavsky
Premature optimization is premature.
Robert Rossney
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
+1  A: 

are you looking for this:

mylist = range(750, 765)
mylist.extend([769, 770, 774])
Vasil
I thought about doing it this way, but I knew there had to be a better, more concise way. Good suggestion though.
adam
yes, the accepted answer is shorter. I thought you needed to store the list for later use.
Vasil
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
@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
Good call Brian, I'm glad I brought that up.
adam
+10  A: 

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
I am not familiar with the itertools library. Thank you for suggesting it. I'll read up on it.
adam
+4  A: 

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)
Jarret Hardie
yeah, and be sure to use brackets with functions!
SilentGhost
Ah, thank you. You have no idea how many times I've gotten a syntax error in Python 3.0 because of the darn print function!
Jarret Hardie