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

    Join Date
    Feb 2011
    Rep Power

    Smile Dictionary and Pickle

    Can someone please explain to me in very simple terms, dictionaries? I am not getting the concept. I am a QBASIC & GWBASIC (years ago out of college) and VB programmer and now I use .Net in my job. Dictionaries in Python remind me of the old basic Data statements? Correct? But examples seem to use them more like database tables. Anyway if you can help me wrap my head around it I would like to understand how to create the dictionary, add data to it, query data and delete data? I guess if I am understanding it...

    Also I need to understand pickles or pickling.

    Any help is greatly appreciated.

    Thank you,
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    Python dictionaries are UNLIKE a BASIC data statement.

    Suppose in BASIC you have an array and you want to access the value at index 2. You'd write A(2) .

    Suppose you wanted the value at index 'dog' . You can't do that in BASIC. You can in python.

    >>> d = dict()    # create a dictionary object
    >>> d['dog'] = 'cat chasing beast'   # populate it
    >>> d['cat'] = 'adorable, fuzzy,  dog-taunting critter'
    >>> print(d['dog'])    # access it.
    cat chasing beast
    >>> d['Monty Python']   # 'Monty Python' isn't a key of this dictionary
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'Monty Python'
    Dictionary keys can be any hashable object. tuples, frozensets, numbers are hashable, as are objects with an __hash__ method. Lists and dictionaries are not hashable because the hash depends on content, and the content of lists and dictionaries can vary.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo