In [ ]:
# dictionaries,dict,hash,hashes
# ssn - Social Security Number
# uin - Unique Identification number
# me(santosh-UID) : address,se
# father(UID)     : address,gs
# mother(UID)     : address,hw
# key:value ( keys should always be unique)

In [3]:
# dictionaries are not index based.
# dictionaries need not be arranged the way they are inserted.
# if you want them to be printed the way you entered then you need to use pprint.

my_fruits = {'a':'apple','b':'banana','c':'choclate','d':'donut'}
print my_fruits,type(my_fruits)
empty_dict = {}
print empty_dict,type(empty_dict)
empty_dict = dict()
print empty_dict,type(empty_dict)


{'a': 'apple', 'c': 'choclate', 'b': 'banana', 'd': 'donut'} <type 'dict'>
{} <type 'dict'>
{} <type 'dict'>

In [8]:
# we can extract the elements of a dictionary using the key and value pair.
print my_fruits['a']
print my_fruits['d']


avacardo
donut

In [6]:
# insert a new value into a dictionary
my_fruits['e'] = 'elephant'
print my_fruits
# replace
my_fruits['a'] = 'avacardo'
print my_fruits


{'a': 'apple', 'c': 'choclate', 'b': 'banana', 'e': 'elephant', 'd': 'donut'}
{'a': 'avacardo', 'c': 'choclate', 'b': 'banana', 'e': 'elephant', 'd': 'donut'}

In [17]:
# iteration on dictionaries
for key in my_fruits:
    print key,my_fruits[key]


a apple
c choclate
b banana
d donut

In [ ]:
# cheat-sheets
# list - ['apple','banana','cherry'],[],list()
# tuple - ('apple','banana','cherry'),(),tuple()
# dict - {'a':'apple','b':'balloon'},{},dict()

In [4]:
# in on a dictionary works only on keys.
print 'a' in my_fruits
print 'apple' in my_fruits


True
False

In [9]:
# functions
my_fruits = {'a':'apple','b':'banana','c':'choclate','d':'donut'}
print dir(my_fruits)


['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']

In [11]:
# get
print my_fruits.get('a')
print my_fruits['a']


apple
apple

In [12]:
# 'has_key'
print 'a' in my_fruits
print my_fruits.has_key('a')


True
True

In [13]:
# keys,iterkeys,viewkeys
# keys

print help(my_fruits.keys)
print my_fruits.keys()


Help on built-in function keys:

keys(...)
    D.keys() -> list of D's keys

None
['a', 'c', 'b', 'd']

In [18]:
# iterkeys
print help(my_fruits.iterkeys)

for key in my_fruits.iterkeys():
    print key


Help on built-in function iterkeys:

iterkeys(...)
    D.iterkeys() -> an iterator over the keys of D

None
a
c
b
d

In [19]:
# viewkeys - templated systems.
# for your frameworks - django,flask and bottle.
print help(my_fruits.viewkeys)
print my_fruits.viewkeys()


Help on built-in function viewkeys:

viewkeys(...)
    D.viewkeys() -> a set-like object providing a view on D's keys

None
dict_keys(['a', 'c', 'b', 'd'])

In [21]:
#values,itervalues,viewvalues

# values
print help(my_fruits.values)
print my_fruits.values()


Help on built-in function values:

values(...)
    D.values() -> list of D's values

None
['apple', 'choclate', 'banana', 'donut']

In [23]:
# itervalues
print help(my_fruits.itervalues)
for value in  my_fruits.itervalues():
    print value


Help on built-in function itervalues:

itervalues(...)
    D.itervalues() -> an iterator over the values of D

None
apple
choclate
banana
donut

In [26]:
# viewvalues - templated systems.
# for your frameworks - django,flask and bottle.
print help(my_fruits.viewvalues)
print my_fruits.viewvalues()
for value in my_fruits.viewvalues():
    print value


Help on built-in function viewvalues:

viewvalues(...)
    D.viewvalues() -> an object providing a view on D's values

None
dict_values(['apple', 'choclate', 'banana', 'donut'])
apple
choclate
banana
donut

In [27]:
# items,iteritems,viewitems

print help(my_fruits.items)
print my_fruits.items()
# [('avinash', 'django'), ('azhar', 'ruby'), ('kumar', 'ansibel'), ('vipin', 'python')]


Help on built-in function items:

items(...)
    D.items() -> list of D's (key, value) pairs, as 2-tuples

None
[('a', 'apple'), ('c', 'choclate'), ('b', 'banana'), ('d', 'donut')]

In [29]:
# iteritems
print help(my_fruits.iteritems)
print my_fruits.iteritems()

for value in my_fruits.iteritems():
    print value


Help on built-in function iteritems:

iteritems(...)
    D.iteritems() -> an iterator over the (key, value) items of D

None
<dictionary-itemiterator object at 0x7fc164635a48>
('a', 'apple')
('c', 'choclate')
('b', 'banana')
('d', 'donut')

In [30]:
# viewitems
print help(my_fruits.viewitems)
print my_fruits.viewitems()


Help on built-in function viewitems:

viewitems(...)
    D.viewitems() -> a set-like object providing a view on D's items

None
dict_items([('a', 'apple'), ('c', 'choclate'), ('b', 'banana'), ('d', 'donut')])

In [36]:
# update
print help(my_fruits.update)
my_fruits.update({'an':'anjeer'})
print my_fruits

my_fruits['ana']='anar'
print my_fruits


Help on built-in function update:

update(...)
    D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
    If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
    If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
    In either case, this is followed by: for k in F: D[k] = F[k]

None
{'a': 'apple', 'c': 'choclate', 'b': 'banana', 'd': 'donut', 'an': 'anjeer'}
{'a': 'apple', 'c': 'choclate', 'b': 'banana', 'd': 'donut', 'ana': 'anar', 'an': 'anjeer'}

In [47]:
# setdefault - task to be looked into.
print help(my_fruits.setdefault)
print my_fruits.setdefault('a',None)
print my_fruits.setdefault('z','zebra')
#print my_fruits['a']
#print my_fruits['z']


Help on built-in function setdefault:

setdefault(...)
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

None
apple
None

In [55]:
# copy
# softcopy,deepcopy,shallowcopy
# shallowcopy always works on complex objects.
import copy

a = [1,2,3]
b = [4,5,6]
Cc = [a,b]


print a,id(a)
print b,id(b)
print Cc,id(Cc),id(Cc[0]),id(Cc[1])
# softcopy
Soc = Cc
print Soc,id(Soc),id(Soc[0]),id(Soc[1])

# deepcopy
Doc = copy.deepcopy(Cc)
print Doc,id(Doc),id(Doc[0]),id(Doc[1])



# shallowcopy
Shc = copy.copy(Cc)
print Shc,id(Shc),id(Shc[0]),id(Shc[1])

# if i modify a
a[0] = "one"
print a
print Shc,id(Shc),id(Shc[0]),id(Shc[1])
print Doc,id(Doc),id(Doc[0]),id(Doc[1])


# web1,web2
# web1(training,timing) # complext object
# web2(training,cost)   # complex obejct
3


[1, 2, 3] 140468589632040
[4, 5, 6] 140468589187440
[[1, 2, 3], [4, 5, 6]] 140468589188520 140468589632040 140468589187440
[[1, 2, 3], [4, 5, 6]] 140468589188520 140468589632040 140468589187440
[[1, 2, 3], [4, 5, 6]] 140468589187368 140468589188880 140468589631392
[[1, 2, 3], [4, 5, 6]] 140468589189888 140468589632040 140468589187440
['one', 2, 3]
[['one', 2, 3], [4, 5, 6]] 140468589189888 140468589632040 140468589187440
[[1, 2, 3], [4, 5, 6]] 140468589187368 140468589188880 140468589631392

In [58]:
# copy
print help(my_fruits.copy)
D_fruits = my_fruits.copy()
print D_fruits
print D_fruits is my_fruits


Help on built-in function copy:

copy(...)
    D.copy() -> a shallow copy of D

None
{'a': 'apple', 'c': 'choclate', 'b': 'banana', 'an': 'anjeer', 'd': 'donut', 'z': None, 'ana': 'anar'}
False

In [63]:
# pop
print help(D_fruits.pop)
print D_fruits
print D_fruits.pop('b')


Help on built-in function pop:

pop(...)
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised

None
{'c': 'choclate', 'b': 'banana', 'an': 'anjeer', 'd': 'donut', 'z': None, 'ana': 'anar'}
banana

In [64]:
print D_fruits
print D_fruits.pop('b')


{'c': 'choclate', 'an': 'anjeer', 'd': 'donut', 'z': None, 'ana': 'anar'}
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-64-74a459eb620f> in <module>()
      1 print D_fruits
----> 2 print D_fruits.pop('b')

KeyError: 'b'

In [66]:
#popitem
print help(D_fruits.popitem)
D_fruits.popitem()


Help on built-in function popitem:

popitem(...)
    D.popitem() -> (k, v), remove and return some (key, value) pair as a
    2-tuple; but raise KeyError if D is empty.

None
Out[66]:
('c', 'choclate')

In [67]:
print D_fruits


{'an': 'anjeer', 'd': 'donut', 'z': None, 'ana': 'anar'}

In [69]:
# clear'
print help(D_fruits.clear)
print D_fruits.clear()
print D_fruits


Help on built-in function clear:

clear(...)
    D.clear() -> None.  Remove all items from D.

None
None
{}

In [ ]:
# fromkeys => examples -> tutorialpoint