Velocity Reviews > Faster way to do this...

# Faster way to do this...

Harlin Seritt
Guest
Posts: n/a

 03-01-2005
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

Will McGugan
Guest
Posts: n/a

 03-01-2005
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

Steve Holden
Guest
Posts: n/a

 03-01-2005
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

Aaron Bingham
Guest
Posts: n/a

 03-01-2005
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
--------------------------------------------------------------------

Roy Smith
Guest
Posts: n/a

 03-01-2005
Harlin Seritt <(E-Mail Removed)> 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.

Warren Postma
Guest
Posts: n/a

 03-01-2005
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

Harlin Seritt
Guest
Posts: n/a

 03-02-2005
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

Robert Kern
Guest
Posts: n/a

 03-02-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

Will McGugan
Guest
Posts: n/a

 03-02-2005
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

Robert Kern
Guest
Posts: n/a

 03-02-2005
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
(E-Mail Removed)

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