Building lists

Larry Hudson orgnut at yahoo.com
Tue Oct 21 09:40:06 CEST 2014


On 10/20/2014 12:49 PM, Seymore4Head wrote:
> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python at mrabarnett.plus.com>
> wrote:
>
      <snip>

> Do you have to know the number of items the list will have before
> making it?
>

No.  Lists are NOT the same as arrays in other languages.  But it IS possible to create an 
initial list of a specific size:

myList = [None] * 50

This creates a 50-element list with each element set to None.  (BTW, that makes the indexes 
0-49, not 0-50.)  I have occasionally found this useful, but I emphasize it is only RARELY 
useful.  The .append() method is far more versatile.

As to your original problem:  my question to you is what is your purpose?

1)  To solve this particular problem, using Python.
   or
2)  To explore the usage of lists by applying them to this problem.

If your purpose is the first, then I agree with the advice you have already been given here -- 
dictionaries are a much better fit to this problem.

If your purpose is the second, then go ahead, have at it.  But realize that to more experienced 
Pythonistas this approach the very un-pythonic for this problem.  It would be even better to try 
multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or 
tuples...  or whatever combination you can come up with.  This will give you even more 
experience and allow you to evaluate these various techniques.

And no, I won't give you a ready-made "canned" answer.  For one thing your original description 
is much too vague.  But good luck!

      -=- Larry -=-




More information about the Python-list mailing list