#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    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)
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Smile reply


    Thank you DevCoach for the example.
    Thank you Grim Archon and Strike for your answer
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    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 ?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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

IMN logo majestic logo threadwatch logo seochat tools logo