# Faster way to do this...

Discussion in 'Python' started by Harlin Seritt, Mar 1, 2005.

1. ### Harlin SerittGuest

I've got the following code:

nums = range(0)
for a in range(100):
nums.append(a)

Is there a better way to have num initialized to a list of 100
consecutive int values?

Thanks,

Harlin

Harlin Seritt, Mar 1, 2005

2. ### Will McGuganGuest

Harlin Seritt wrote:
> I've got the following code:
>
> nums = range(0)
> for a in range(100):
> nums.append(a)
>
> Is there a better way to have num initialized to a list of 100
> consecutive int values?
>

Isn't that equivalent to simply..

nums= range(100)

Will McGugan

Will McGugan, Mar 1, 2005

3. ### Steve HoldenGuest

Harlin Seritt wrote:
> I've got the following code:
>
> nums = range(0)
> for a in range(100):
> nums.append(a)
>
> Is there a better way to have num initialized to a list of 100
> consecutive int values?
>

Why not the simplest solution?

a = range(100)

regards
Steve

Steve Holden, Mar 1, 2005
4. ### Aaron BinghamGuest

Harlin Seritt wrote:
> I've got the following code:
>
> nums = range(0)
> for a in range(100):
> nums.append(a)
>
> Is there a better way to have num initialized to a list of 100
> consecutive int values?

You mean like this?

nums = range(100)

;-)

--
--------------------------------------------------------------------
Aaron Bingham
Software Engineer
Cenix BioScience GmbH
--------------------------------------------------------------------

Aaron Bingham, Mar 1, 2005
5. ### Roy SmithGuest

Harlin Seritt <> wrote:
>I've got the following code:
>
>nums = range(0)
>for a in range(100):
> nums.append(a)
>
>Is there a better way to have num initialized to a list of 100
>consecutive int values?

Step one would be to change the first line to

nums = []

which is simpler and results in the same thing. Or, you could write
the whole thing as a one-liner using a list comprehension

nums = [a for a in range(100)]

and then you can take it one step further and just write

nums = range(100)

which I think is about as simple as you can get.

Roy Smith, Mar 1, 2005
6. ### Warren PostmaGuest

Will McGugan wrote:

> Isn't that equivalent to simply..
>
> nums= range(100)

I remember the day I first realized that 900 lines of some C++ program I
was working on could be expressed in three lines of python. Ahh.
Rebirth. Then there was the phase of the python-newbie so enamored of
map and lambda. ... Wait, actually, I'm not out of that yet.

Warren

Warren Postma, Mar 1, 2005
7. ### Harlin SerittGuest

Excellent point Warren. I have been working with Python for about 3
years in all, but only really seriously for about a year. I am still
utterly amazed that near everything that takes me about 5 to 20 lines
of code can be done in 1, 2 or 3 lines of Python code (when done
correctly). It is very frustrating that I am still using Python as
though I would if I were writing Java or C++ code. One day I'll get the
hang of this.

Roy, I like what you showed: nums = [a for a in range(100)] . My
mistake for not expressing my question as well as I should have. Not
only am I looking for a way to fill in 100 spots (more or less) in an
array errrrr... list, but I'd like to be able to do it in intervals of
2, 4, 8 etc. as well as other things.

Thanks,

Harlin

Harlin Seritt, Mar 2, 2005
8. ### Robert KernGuest

Harlin Seritt wrote:

> Roy, I like what you showed: nums = [a for a in range(100)] . My
> mistake for not expressing my question as well as I should have. Not
> only am I looking for a way to fill in 100 spots (more or less) in an
> array errrrr... list, but I'd like to be able to do it in intervals of
> 2, 4, 8 etc. as well as other things.

Like

nums = range(0, 100, 4)

?

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, Mar 2, 2005
9. ### Will McGuganGuest

Warren Postma wrote:
> Will McGugan wrote:
>
>> Isn't that equivalent to simply..
>>
>> nums= range(100)

>
>
> I remember the day I first realized that 900 lines of some C++ program I
> was working on could be expressed in three lines of python. Ahh.

Lately I've found myself commenting C++ code with the equivalent Python
code. I find it clearer and more terse than simply commenting in English!

Will

Will McGugan, Mar 2, 2005
10. ### Robert KernGuest

Will McGugan wrote:
> Warren Postma wrote:
>
>> Will McGugan wrote:
>>
>>> Isn't that equivalent to simply..
>>>
>>> nums= range(100)

>>
>>
>>
>> I remember the day I first realized that 900 lines of some C++ program
>> I was working on could be expressed in three lines of python. Ahh.

>
>
> Lately I've found myself commenting C++ code with the equivalent Python
> code. I find it clearer and more terse than simply commenting in English!

If you used literate programming tools, you might be able to get a
Python version and a C++ version of your code in one go!

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, Mar 2, 2005
11. ### Timo VirkkalaGuest

Harlin Seritt wrote:
> Roy, I like what you showed: nums = [a for a in range(100)] . My
> mistake for not expressing my question as well as I should have. Not
> only am I looking for a way to fill in 100 spots (more or less) in an
> array errrrr... list, but I'd like to be able to do it in intervals of
> 2, 4, 8 etc. as well as other things.

range(2, 100, 4)

How about you fire up the interactive python and try
help(range)

--
Timo Virkkala

Timo Virkkala, Mar 3, 2005