Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Gregory Ewing Newsgroups: comp.lang.python Subject: Re: Building lists Date: Tue, 21 Oct 2014 10:54:55 +1300 Lines: 35 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net RxmxfSTsToDOLyJyhZcPYwLEeC08WWCWR/SnJSA0PMIBMW7Zix Cancel-Lock: sha1:87Xdt3NPc5CFnkWuG7y+cbcsvkI= User-Agent: Mozilla Thunderbird 1.0.5 (Macintosh/20050711) X-Accept-Language: en-us, en In-Reply-To: Xref: csiph.com comp.lang.python:79341 Seymore4Head wrote: > Say I want a > Grocery list and it will have a maximum size of 50 items. In Python it's actually easier to deal with variable-sized lists having no maximum size. Instead of pre-creating a list of a given size, just start with an empty list and append things to it. Unless there's some external reason for a size limit (e.g. you can't fit more than 50 items in your shopping bag) there's no need to impose limit at all. You should certainly start without a limit and only add it later if needed. Some things to get you started: items = [] # Start with an empty list items.append("cheese") # Add some items to it items.append("eggs") items.append("milk") for item in items: # Process the items print(item) If you really need to limit the number of items, you can do something like this: def add_item(item): if len(items) < 50: items.append(item) else: raise Exception("Shopping list is full") -- Greg