TIP: What is the max length of a shelve key or value?

Answer: I don’t really know.

With shelve, I used a key length of 72 with no error. I used a value length of 5000 with no error. If you have larger data needs then sqlite3 is built into Python. Try that.

I could not find any documentation on this and I looked at shelve docs, which uses pickle, so I looked at the pickle docs, which uses anydbm (or dbm depending on where you read about it), and none of them talk about the max length.

However back in my Perl days the max length of the key + value when using dbm was about 1000 bytes. So I wrote this program to test the max length. I used a value with 5000 bytes and still got no error.

r""" Test shelve module for max length of key and values.
Created 4/13/2024 by bacca400@protonmail.com

Install these module: py -m pip install xxx (shelve comes with Python already)
v1.0.0

This tests to see how long a key or value can be for the shelve module, which uses pickle, which uses the anydbm module. I could not find any documentation for the max length of the key or value for these so I wrote this program. If you get an error then you have exceeded the max length of either the key or value. 
"""

import inspect
import sys

import shelve
import random 
import string # Needed to generate random string lengths.

#####################################################
# Functions
#####################################################
#####################################################
def mymain():
    # Do stuff here.
    ctrs = string.ascii_uppercase + string.digits
    longkey="dlkajd as;ldf as;ldkfjas;lk jfal kja;slj fa;jf a;j ;jkjdshljkadfhlfakhl"
    longval = "".join(random.choices(ctrs, k=1000))
    longkey2 = 'key2'
    longval2 = "".join(random.choices(ctrs, k=2000))
    longkey3 = 'key3'
    longval3 = "".join(random.choices(ctrs, k=5000))

    dbfn = "shelve.db"
    print(f"Longkey len=",len(longkey))
    print(f"Longval len=",len(longval))
    with shelve.open(dbfn) as db: 
        db[longkey]=longval
        print(f"Stored Key len={len(longkey)}: {longkey} with value length of: {len(longval)}")
        db[longkey2] = longval2
        print(f"Stored Key {longkey2} with value length: {len(longval2)}")
        db[longkey3] = longval3
        print(f"Stored Key {longkey3} with value length of: {len(longval3)}")

    # db is closed automagically. 
    print("")
    with shelve.open(dbfn) as db: 
        t = db[longkey]
        print(f"Retrieved long key: {longkey}")
        t = db[longkey2]
        print(f"Retrieved long key: {longkey2}")
        t = db[longkey3]
        print(f"Retrieved long key: {longkey3}")
        # print(f"\nPrinting dict:",dict(db))
    
#####################################################
# Main program
if __name__ == "__main__": 
    mymain()

You can use this program to do your own testing.

@c-rob According to the documentation the keys in a shelf are “ordinary strings” (line 4 in the first paragraph), and as far as I know string lengths in Python 3 are only limited by the available memory.

P. S. I did not see the reference to values in the OP - the same would apply to shelf values, which are described as being “arbitrary Python objects”. How big can a Python object be? As big as your available memory. I’ve never used shelf, but have used pickle.