In [ ]:
# dictionaries
# key => value
# adhar
# mom :<uin>(key-unique):home(value),lastname(value),bankaccount
# dad :<uin>(key):home,lastname,bankaccount
# kiddo:<uin>(key):home,lastname,bankaccount

In [ ]:
# cheatsheet
# list - ['apple','banana'],list(),[]
# tuple - ('apple','banana'),tuple(),()
# dict -  {'a':'apple','b':'banana'},dict(),{}

In [1]:
my_fruits = {'a':'apple','b':'banana','c':'cherry'}
empty_dict = {}
empty_dict1 = dict()

In [4]:
print my_fruits,type(my_fruits)
print empty_dict,type(empty_dict)
print empty_dict1,type(empty_dict1)


{'a': 'apple', 'c': 'cherry', 'b': 'banana'} <type 'dict'>
{} <type 'dict'>
{} <type 'dict'>

In [5]:
# extraction of values
my_fruits['a']


Out[5]:
'apple'

In [6]:
# inserting values into dict
my_fruits['d']='dates'
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates'}

In [7]:
# replace
my_fruits['a'] = 'apricot'
print my_fruits


{'a': 'apricot', 'c': 'cherry', 'b': 'banana', 'd': 'dates'}

In [3]:
#functions

print my_fruits
print dir(my_fruits)


{'a': 'apple', 'c': 'cherry', 'b': 'banana'}
['__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 [ ]:
# TASKS
# update,setdefault,fromkeys
# tutorialpoint

In [6]:
# has_key => in
# in operator is only true for a key.
print 'a' in my_fruits
print 'apple' in my_fruits

print my_fruits.has_key('a')


True
False
True

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


apple
apple

In [9]:
# items,iteritems,viewitems

print help(my_fruits.items)


Help on built-in function items:

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

None

In [10]:
print my_fruits
print my_fruits.items()


{'a': 'apple', 'c': 'cherry', 'b': 'banana'}
[('a', 'apple'), ('c', 'cherry'), ('b', 'banana')]

In [12]:
print help(my_fruits.iteritems)


Help on built-in function iteritems:

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

None

In [13]:
print my_fruits.iteritems()


<dictionary-itemiterator object at 0x7fceb5f0eaa0>

In [15]:
for key,value in my_fruits.iteritems():
    print key,value


a apple
c cherry
b banana

In [16]:
# viewitems

print help(my_fruits.viewitems)


Help on built-in function viewitems:

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

None

In [17]:
# template data for framework templates
print my_fruits.viewitems()


dict_items([('a', 'apple'), ('c', 'cherry'), ('b', 'banana')])

In [18]:
# keys,iterkeys,viewkeys

print help(my_fruits.keys)


Help on built-in function keys:

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

None

In [19]:
print my_fruits.keys()


['a', 'c', 'b']

In [20]:
print help(my_fruits.iterkeys)


Help on built-in function iterkeys:

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

None

In [21]:
print my_fruits.iterkeys()


<dictionary-keyiterator object at 0x7fceb5f28158>

In [22]:
for key in my_fruits.iterkeys():
    print key


a
c
b

In [23]:
print help(my_fruits.viewkeys)


Help on built-in function viewkeys:

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

None

In [24]:
print my_fruits.viewkeys()


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

In [ ]:
# values,itervalues,viewvalues

In [ ]:
## copy
#  -softcopy
#  -deepcopy
#  -shallowcopy

In [25]:
# shallowcopy -> complex objects

a = [1,2,3]
b = [4,5,6]
print a,id(a),b,id(b)


[1, 2, 3] 140525801438240 [4, 5, 6] 140526175555664

In [26]:
Cc = [a,b]
print Cc,id(Cc)
print Cc[0],id(Cc[0]),Cc[1],id(Cc[1])


[[1, 2, 3], [4, 5, 6]] 140525801451816
[1, 2, 3] 140525801438240 [4, 5, 6] 140526175555664

In [27]:
# Softcopy

Sc = Cc
print Sc,id(Sc)
print Sc[0],id(Sc[0]),Sc[1],id(Sc[1])


[[1, 2, 3], [4, 5, 6]] 140525801451816
[1, 2, 3] 140525801438240 [4, 5, 6] 140526175555664

In [28]:
# Deepcopy
import copy
Dc = copy.deepcopy(Cc)
print Dc,id(Dc)
print Dc[0],id(Dc[0]),Dc[1],id(Dc[1])


[[1, 2, 3], [4, 5, 6]] 140525792504216
[1, 2, 3] 140525792505368 [4, 5, 6] 140525801445784

In [29]:
# shallow copy
import copy
Ss = copy.copy(Cc)
print Ss,id(Ss)
print Ss[0],id(Ss[0]),Ss[1],id(Ss[1])


[[1, 2, 3], [4, 5, 6]] 140525792571616
[1, 2, 3] 140525801438240 [4, 5, 6] 140526175555664

In [ ]:
# page1 -> [traiings,timing]
# page2 -> [trainigs,cost]

In [31]:
# copy
print my_fruits
print help(my_fruits.copy)


{'a': 'apple', 'c': 'cherry', 'b': 'banana'}
Help on built-in function copy:

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

None

In [32]:
Dc = my_fruits.copy()

In [33]:
print Dc,id(Dc)
print my_fruits,id(my_fruits)


{'a': 'apple', 'c': 'cherry', 'b': 'banana'} 140525801416976
{'a': 'apple', 'c': 'cherry', 'b': 'banana'} 140525801479256

In [35]:
# clear
Dc.clear()
print Dc


{}

In [36]:
# pop,popitem
print help(my_fruits.pop)
print help(my_fruits.popitem)


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
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

In [37]:
print my_fruits.pop('a')


apple

In [39]:
print my_fruits.pop('a')


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-39-5d189acd1ac8> in <module>()
----> 1 print my_fruits.pop('a')

KeyError: 'a'

In [38]:
print my_fruits


{'c': 'cherry', 'b': 'banana'}

In [41]:
my_fruits.popitem()


Out[41]:
('c', 'cherry')

In [42]:
print my_fruits


{'b': 'banana'}

In [43]:
print my_fruits.pop()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-de3dc57389ab> in <module>()
----> 1 print my_fruits.pop()

TypeError: pop expected at least 1 arguments, got 0

In [ ]: