Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:44:11+00:00 2026-05-27T19:44:11+00:00

This is a problem I’ve come across a lot lately. Google doesn’t seem to

  • 0

This is a problem I’ve come across a lot lately. Google doesn’t seem to have an answer so I bring it to the good people of stack overflow.

I am looking for a simple way to populate a list with the output of a function. Something like this:

fill(random.random(), 3) #=> [0.04095623, 0.39761869, 0.46227642]

Here are other ways I’ve found to do this. But I’m not really happy with them, as they seem inefficient.

results = []
for x in xrange(3): results.append(random.random())
#results => [0.04095623, 0.39761869, 0.46227642]

and

map(lambda x: random.random(), [None] * 3)
#=> [0.04095623, 0.39761869, 0.46227642]

Suggestions?


Thanks for all the answers. I knew there was a more python-esque way.

And to the efficiency questions…

$ python --version
Python 2.7.1+
$ python -m timeit "import random" "map(lambda x: random.random(), [None] * 3)"
1000000 loops, best of 3: 1.65 usec per loop
$ python -m timeit "import random" "results = []" "for x in xrange(3): results.append(random.random())"
1000000 loops, best of 3: 1.41 usec per loop
$ python -m timeit "import random" "[random.random() for x in xrange(3)]"
1000000 loops, best of 3: 1.09 usec per loop
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:44:11+00:00Added an answer on May 27, 2026 at 7:44 pm

    How about a list comprehension?

    [random.random() for x in xrange(3)]
    

    Also, in many cases, you need the values just once. In these cases, a generator expression which computes the values just-in-time and does not require a memory allocation is preferable:

    results = (random.random() for x in xrange(3))
    for r in results:
       ...
    # results is "used up" now.
    # We could have used results_list = list(results) to convert the generator
    

    By the way, in Python 3.x, xrange has been replaced by range. In Python 2.x, range allocates the memory and calculates all values beforehand (like a list comprehension), whereas xrange calculates the values just-in-time and does not allocate memory (it’s a generator).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem doesn't seem browser specific and generally applies to style in the webkit/gecko/mozilla
This problem must have been solved a million times, but Google has not been
This problem seems to come up for a few people, but I haven't been
This problem has been kicking my butt for a few days now. I have
This problem has bugged me so many times and i have now decided to
This problem has been driving me mad. Here's the general gist: I have two
This problem is making me crazy. Actually I have multiple problems. First one: Why
This problem smells like there should be an answer in graph theory, but it
This problem is giving me a real headache. I have two tables in my
This problem exists only in IE browser, in mozilla its working fine... i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.