List limits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob_smith_17280@hotmail.com

    #1

    List limits

    How many items can be stored in a Python list? I have close to 70,000
    items... is this within a lists limits?

  • Christopher De Vries

    #2
    Re: List limits

    It is possible to store 70,000 items in a list (try "l =
    range(70000)"), but the best way to check if you can store all the
    items you need to store is to try it. After all if they are all very
    large you might potentially run out of memory.

    Chris

    Comment

    • Jeff Epler

      #3
      Re: List limits

      I'm referring to Python 2.2's C headers as I answer this question. I
      believe some of may have changed by 2.4.

      The number of elements in a "variable-sized object" (those with
      Py_VAR_HEAD; I believe this includes lists, tuples, and strings) is
      stored in a platform 'int'.

      On most (desktop) systems, this means the limit of any sized object is
      no more than 2**31-1, or about 2 billion.

      On those same systems, a list object of length 128 million, give or
      take, approximately 512 megabytes of memory will be allocated for the
      list object itself (4 bytes for each pointer-to-element). If each
      element of the list is distinct, those objects will each require
      additional memory---The smallest useful Python object takes around 16
      bytes, IIRC, which would bring the total memory required to around 2560
      megabytes if I didn't screw up my arithmetic. This is close to (or
      over, depending on the system) the maximum amount of physical RAM these
      machines can accomodate, and the maximum amount of address space
      available to a single program.

      While performing list-resizing operations, there may be a temporary need
      for two copies of the list object itself, bumping the memory used up to 3
      gigs.

      Finally, the speed of some operations (l.index(item), l.pop(0),
      l.insert(0, item)) are related linearly to the size of the list, so your
      program may slow down as the lists it manipulates grow. Others, such as
      l[i], l.pop(), and l.append(item), are constant-time or amortized-constant-
      time.

      Jeff

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.2.1 (GNU/Linux)

      iD8DBQFBxw6pJd0 1MZaTXX0RAljUAJ oD8/bR3hBgQOb3I40h+ yKm5r9PqQCdEiBo
      s1rX4NrllE9a+vW oQQpqFe4=
      =F8Db
      -----END PGP SIGNATURE-----

      Comment

      • Raymond Hettinger

        #4
        Re: List limits

        > How many items can be stored in a Python list? I have close to 70,000[color=blue]
        > items... is this within a lists limits?[/color]

        Lists store pointers to objects. Unless you have a lot of duplicates, it is the
        objects themselves that will consume most of your memory. The list itself will
        likely be small in comparison.


        Raymond Hettinger


        Comment

        • Nick Coghlan

          #5
          Re: List limits

          Raymond Hettinger wrote:[color=blue][color=green]
          >>How many items can be stored in a Python list? I have close to 70,000
          >>items... is this within a lists limits?[/color]
          >
          >
          > Lists store pointers to objects. Unless you have a lot of duplicates, it is the
          > objects themselves that will consume most of your memory. The list itself will
          > likely be small in comparison.[/color]

          Given the size of the counter, is it actually physically possible for a list to
          run out of room before the application runs out memory?

          Even list(None for x in xrange(sys.maxi nt)) wouldn't do the trick, since each of
          those pointers to None is taking 4 bytes of memory, and Python's internal
          structures are already chewing up some of the address space.

          Cheers,
          Nick.

          --
          Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
          ---------------------------------------------------------------

          Comment

          • Fredrik Lundh

            #6
            Re: List limits

            Nick Coghlan wrote:
            [color=blue]
            > Given the size of the counter, is it actually physically possible for a list to run out of room
            > before the application runs out memory?[/color]

            depends on the system architecture, of course: consider an 64-bit computer
            with 32-bit integers and 256 GB of memory...

            </F>



            Comment

            Working...