[Tutor] Alternatives to append() for "growing" a list

Danny Yoo dyoo at hashcollision.org
Sun Dec 8 07:21:57 CET 2013


>
>
> It didn't have to do with strings. It was a basic example of using
> append() which is to start with an empty list and and then build it
> incrementally:
>
> >>> l = [ ]
> >>> l.append(1)
> # append more
>
>
Hi Amit,


Ok, good.  This context helps!

If you do know all the values of the list up front, then defining 'f' with
those values as part of the list literal is idiomatic:

    l = [1,
          ## fill me in...
         ]

and in this way, we probably wouldn't use append() for this situation.  The
reason for this can be based on readability arguments: a programmer who
sees this will be more inclined to know that the list won't change.

Symmetrically, the presence of 'l.append()' in a program is often a hint a
reader to anticipate the need for the list to have some dynamic,
runtime-dependent size.

Good luck!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131207/542a6c16/attachment.html>


More information about the Tutor mailing list