|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Is there a default size limit to a dictionary, list or array declaration ?
Is there a default size limit to a dictionary, list or array declaration ?
===================== When I declared a dictionary and a list in Python, there is no where I could specified how big this dictionary or list can be such as declaring an array in C. ex: char array[1000] in C programming. 1) Is there a default size limit to a dictionary, or list declaration in Python (ex: 64 Kb or how many elements) ? 2) Is there a default size limit to an array declaration in Python (ex: 64 Kb or how many elements)? ===================== dictionary = {'food':{'beef':1, 'egg':2}, 'drink':{'apple juice':1, 'grape juice':2}} list = ['123','456','789'] |
|
#2
|
|||
|
|||
|
Quote:
No, they can be as big as your memory allows. If you need to limit the size, you'll either have to write a wrapper class, or do some size-checking for every addition to the data structure (I recommend the wrapper class approach) |
|
#3
|
||||
|
||||
|
Python uses dynamic memory allocation so you don't specify the size of an object. It will use as little or as much as you need. Any name can be assigned any object at run time, that object can be dynamically generated (e.g. a complete file read as a string).
It is another example of what makes Python a RAD langauge - it lets you code the solution to the problem - you don't normally have to worry about housekeeping. If you want to find out how efficiently this is done I guess you will need to read the sources ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#4
|
|||
|
|||
|
What Grim & strike said. Be aware though that if you are adding elements to a list it has to reallocate the memory as the list grows, which can slow things down. If you know the list is going to reach a certain size eventually you can save processing time by pre-allocating it using [None]*size.
e.g. instead of: Code:
lst = []
for i in xrange(1000000):
lst.append('stuff')
you can do: Code:
lst = [None] * 1000000 for i in xrange(1000000): lst[i] = 'stuff' On my system the first took 2.88 seconds, while the second took 0.75 seconds. Dave - The Developers' Coach Last edited by DevCoach : June 8th, 2004 at 02:17 AM. Reason: timings added |
|
#5
|
|||
|
|||
|
Thank you DevCoach for the example.
Thank you Grim Archon and Strike for your answer |
|
#6
|
|||
|
|||
|
reply to DevCoach
Hi DevCoach.
Based on your second example, if a list is predefine with 1,000,000 elements and if it went over that number, then will Python automatically allocate new memory to it or will it crash ? |
|
#7
|
|||
|
|||
|
Quote:
It will allocate new memory for it. Dave - The Developers' Coach |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Is there a default size limit to a dictionary, list or array declaration ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|