The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Is there a default size limit to a dictionary, list or array declaration ?
Discuss Is there a default size limit to a dictionary, list or array declaration ? in the Python Programming forum on Dev Shed. Is there a default size limit to a dictionary, list or array declaration ? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 7th, 2004, 05:47 PM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
|
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']
|

June 7th, 2004, 07:49 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
Quote: | Originally Posted by linh 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'] |
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)
|

June 7th, 2004, 08:02 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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 
|

June 8th, 2004, 12:26 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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 01:17 AM.
Reason: timings added
|

June 8th, 2004, 11:39 AM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
reply
Thank you DevCoach for the example.
Thank you Grim Archon and Strike for your answer
|

June 8th, 2004, 11:47 AM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 245
Time spent in forums: 11 m 27 sec
Reputation Power: 10
|
|
|
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 ?
|

June 8th, 2004, 12:22 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Quote: | Originally Posted by linh 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 ? |
It will allocate new memory for it.
Dave - The Developers' Coach
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|