December 16th, 2012, 02:09 PM
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.
December 16th, 2012, 03:35 PM
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.
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.
>>> 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'
[/code] are essential for python code and Makefiles!