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 677805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:04:47+00:00 2026-05-14T01:04:47+00:00

Say I have a function func(i) that creates an object for an integer i,

  • 0

Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what’s the fastest way to create a list (not a range) equal to this list

mylist = [func(i) for i in range(N)]

without resorting to advanced methods like creating a function in C? My main concern with the above list comprehension is that I’m not sure if python knows beforehand the length of range(N) to preallocate mylist, and therefore has to incrementally reallocate the list. Is that the case or is python clever enough to allocate mylist to length N first and then compute it’s elements? If not, what’s the best way to create mylist? Maybe this?

mylist = [None]*N
for i in range(N): mylist[i] = func(i)

RE-EDIT: Removed misleading information from a previous edit.

  • 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-14T01:04:47+00:00Added an answer on May 14, 2026 at 1:04 am

    Somebody wrote: “””Python is smart enough. As long as the object you’re iterating over has a __len__ or __length_hint__ method, Python will call it to determine the size and preallocate the array.”””

    As far as I can tell, there is no preallocation in a list comprehension. Python has no way of telling from the size of the INPUT what the size of the OUTPUT will be.

    Look at this Python 2.6 code:

    >>> def foo(func, iterable):
    ...     return [func(i) for i in iterable]
    ...
    >>> import dis; dis.dis(foo)
      2           0 BUILD_LIST               0 #### build empty list
                  3 DUP_TOP
                  4 STORE_FAST               2 (_[1])
                  7 LOAD_FAST                1 (iterable)
                 10 GET_ITER
            >>   11 FOR_ITER                19 (to 33)
                 14 STORE_FAST               3 (i)
                 17 LOAD_FAST                2 (_[1])
                 20 LOAD_FAST                0 (func)
                 23 LOAD_FAST                3 (i)
                 26 CALL_FUNCTION            1
                 29 LIST_APPEND      #### stack[-2].append(stack[-1]); pop()
                 30 JUMP_ABSOLUTE           11
            >>   33 DELETE_FAST              2 (_[1])
                 36 RETURN_VALUE
    

    It just builds an empty list, and appends whatever the iteration delivers.

    Now look at this code, which has an ‘if’ in the list comprehension:

    >>> def bar(func, iterable):
    ...     return [func(i) for i in iterable if i]
    ...
    >>> import dis; dis.dis(bar)
      2           0 BUILD_LIST               0
                  3 DUP_TOP
                  4 STORE_FAST               2 (_[1])
                  7 LOAD_FAST                1 (iterable)
                 10 GET_ITER
            >>   11 FOR_ITER                30 (to 44)
                 14 STORE_FAST               3 (i)
                 17 LOAD_FAST                3 (i)
                 20 JUMP_IF_FALSE           17 (to 40)
                 23 POP_TOP
                 24 LOAD_FAST                2 (_[1])
                 27 LOAD_FAST                0 (func)
                 30 LOAD_FAST                3 (i)
                 33 CALL_FUNCTION            1
                 36 LIST_APPEND
                 37 JUMP_ABSOLUTE           11
            >>   40 POP_TOP
                 41 JUMP_ABSOLUTE           11
            >>   44 DELETE_FAST              2 (_[1])
                 47 RETURN_VALUE
    >>>
    

    The same code, plus some code to avoid the LIST_APPEND.

    In Python 3.X, you need to dig a little deeper:

    >>> import dis
    >>> def comprehension(f, iterable): return [f(i) for i in iterable]
    ...
    >>> dis.dis(comprehension)
      1           0 LOAD_CLOSURE             0 (f)
                  3 BUILD_TUPLE              1
                  6 LOAD_CONST               1 (<code object <listcomp> at 0x00C4B8D
    8, file "<stdin>", line 1>)
                  9 MAKE_CLOSURE             0
                 12 LOAD_FAST                1 (iterable)
                 15 GET_ITER
                 16 CALL_FUNCTION            1
                 19 RETURN_VALUE
    >>> dis.dis(comprehension.__code__.co_consts[1])
      1           0 BUILD_LIST               0
                  3 LOAD_FAST                0 (.0)
            >>    6 FOR_ITER                18 (to 27)
                  9 STORE_FAST               1 (i)
                 12 LOAD_DEREF               0 (f)
                 15 LOAD_FAST                1 (i)
                 18 CALL_FUNCTION            1
                 21 LIST_APPEND              2
                 24 JUMP_ABSOLUTE            6
            >>   27 RETURN_VALUE
    >>>
    

    It’s the same old schtick: start off with building an empty list, then iterate over the iterable, appending to the list as required. I see no preallocation here.

    The optimisation that you are thinking about is used inside a single opcode e.g. the implementation of list.extend(iterable) can preallocate if iterable can accurately report its length. list.append(object) is given a single object, not an iterable.

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Emgu is a nice C# port of OpenCV. I'm not… May 14, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer Not a fully answer to your question, but just part… May 14, 2026 at 11:54 pm
  • Editorial Team
    Editorial Team added an answer The version of the Mac OS X Reference Library in… May 14, 2026 at 11:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.