In [2]:
# dictionaries,hashes,dict
# key => value
# adhar,ssn

In [3]:
my_fruits = {'a':'apple','b':'banana','c':'cherry','d':'dates'}

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


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

In [12]:
my_empty = {}
print my_empty,type(my_empty)


{} <type 'dict'>

In [13]:
my_empty = dict()
print my_empty,type(my_empty)


{} <type 'dict'>

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

In [5]:
# listing elements of dictionary
print my_fruits['a']


apple

In [7]:
# append
my_fruits['g'] = ['guava','grapes']
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes']}

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


{'a': 'apricot', 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes']}

In [11]:
my_fruits['a'] = ['apple','apple']
print my_fruits


{'a': ['apple', 'apple'], 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes']}

In [16]:
# in operation
print 'cherry' in my_fruits
print 'c' in my_fruits


False
True

In [18]:
# looping in dictionaries
for key in my_fruits:
    print key


a
c
b
d
g

In [19]:
for key in my_fruits:
    print key,my_fruits[key]


a ['apple', 'apple']
c cherry
b banana
d dates
g ['guava', 'grapes']

In [20]:
# functions

In [22]:
print my_fruits
print dir(my_fruits)


{'a': ['apple', 'apple'], 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes']}
['__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 [29]:
# has_key
print help(my_fruits.has_key)
print 'a' in my_fruits
print my_fruits.has_key('a')


Help on built-in function has_key:

has_key(...)
    D.has_key(k) -> True if D has a key k, else False

None
True
True

In [30]:
# get
print help(my_fruits.get)
print my_fruits['g']
print my_fruits.get('g')


Help on built-in function get:

get(...)
    D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

None
['guava', 'grapes']
['guava', 'grapes']

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


Help on built-in function fromkeys:

fromkeys(...)
    dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
    v defaults to None.

None

In [62]:
students={}
new_students = ('bishwa','arun','varun','vishnu')
print students.fromkeys(new_students)
print students.fromkeys(new_students,'python')
students = students.fromkeys(new_students,'python')
print students


{'arun': None, 'vishnu': None, 'bishwa': None, 'varun': None}
{'arun': 'python', 'vishnu': 'python', 'bishwa': 'python', 'varun': 'python'}
{'arun': 'python', 'vishnu': 'python', 'bishwa': 'python', 'varun': 'python'}

In [34]:
# keys,iterkeys,viewkeys

In [37]:
# 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', 'g']

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


Help on built-in function iterkeys:

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

None

In [39]:
print my_fruits.iterkeys


<built-in method iterkeys of dict object at 0x7fae6c59d7f8>

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


a
c
b
d
g

In [41]:
# viewkeys
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 [42]:
print my_fruits.viewkeys()


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

In [43]:
# values,itervalue,viewvalues

In [44]:
#values
print help(my_fruits.values)


Help on built-in function values:

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

None

In [45]:
print my_fruits.values()


[['apple', 'apple'], 'cherry', 'banana', 'dates', ['guava', 'grapes']]

In [47]:
# itervalues
print help(my_fruits.itervalues)
print 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
<dictionary-valueiterator object at 0x7fae6c531520>
['apple', 'apple']
cherry
banana
dates
['guava', 'grapes']

In [48]:
# viewvalues
print help(my_fruits.viewvalues)
print my_fruits.viewvalues()


Help on built-in function viewvalues:

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

None
dict_values([['apple', 'apple'], 'cherry', 'banana', 'dates', ['guava', 'grapes']])

In [ ]:
# items,viewitems,iteritems

In [49]:
# items
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 [50]:
print my_fruits.items()


[('a', ['apple', 'apple']), ('c', 'cherry'), ('b', 'banana'), ('d', 'dates'), ('g', ['guava', 'grapes'])]

In [52]:
# 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 0x7fae6c531838>
('a', ['apple', 'apple'])
('c', 'cherry')
('b', 'banana')
('d', 'dates')
('g', ['guava', 'grapes'])

In [53]:
# 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', 'apple']), ('c', 'cherry'), ('b', 'banana'), ('d', 'dates'), ('g', ['guava', 'grapes'])])

In [54]:
# update
print help(my_fruits.update)


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

In [55]:
my_fruits.update({'j':'jackfruit','k':'kiwi'})

In [56]:
print my_fruits


{'a': ['apple', 'apple'], 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes'], 'k': 'kiwi', 'j': 'jackfruit'}

In [57]:
my_fruits['l'] = 'leechi'
print my_fruits


{'a': ['apple', 'apple'], 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes'], 'k': 'kiwi', 'j': 'jackfruit', 'l': 'leechi'}

In [58]:
# setdefault
print help(my_fruits.setdefault)


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

In [71]:
print students
# assignment
students.setdefault('kumar','django')
print students
# get - TODO
print students.setdefault('kumar')


{'arun': 'python', 'kumar': 'django', 'vishnu': 'python', 'bishwa': 'python', 'varun': 'python'}
{'arun': 'python', 'kumar': 'django', 'vishnu': 'python', 'bishwa': 'python', 'varun': 'python'}
django
django
django

In [ ]:
# copy - shallowcopy - complex object

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


[1, 2, 3] 140387118869464
[4, 5, 6] 140387118869968
[[1, 2, 3], [4, 5, 6]] 140387118304720

In [76]:
# soft copy
Soc = Cc
print Cc,id(Cc)
print Soc,id(Soc)
print Cc is Soc

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


[[1, 2, 3], [4, 5, 6]] 140387118304720
[[1, 2, 3], [4, 5, 6]] 140387118304720
True
140387118869464 140387118869968
140387118869464 140387118869968
140387118869464 140387118869968

In [77]:
# deep copy
import copy
Doc = copy.deepcopy(Cc)
print Cc,id(Cc)
print Doc,id(Doc)
print Cc is Doc

# task
print id(a),id(b)
print id(Cc[0]),id(Cc[1])
print id(Doc[0]),id(Doc[1])


[[1, 2, 3], [4, 5, 6]] 140387118304720
[[1, 2, 3], [4, 5, 6]] 140387118430688
False
140387118869464 140387118869968
140387118869464 140387118869968
140387118450736 140387118450808

In [79]:
# shallow copy
import copy
print help(copy.copy)


Help on function copy in module copy:

copy(x)
    Shallow copy operation on arbitrary Python objects.
    
    See the module's __doc__ string for more info.

None

In [82]:
Sho = copy.copy(Cc)
print id(Sho),id(Cc)
print Sho is Cc # false
# task
print id(a),id(b)
print id(Cc[0]),id(Cc[1])
print id(Sho[0]),id(Sho[1])


140387118456984 140387118304720
False
140387118869464 140387118869968
140387118869464 140387118869968
140387118869464 140387118869968

In [83]:
print my_fruits


{'a': ['apple', 'apple'], 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes'], 'k': 'kiwi', 'j': 'jackfruit', 'l': 'leechi'}

In [84]:
Cmy_fruits = my_fruits.copy()
print Cmy_fruits


{'a': ['apple', 'apple'], 'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes'], 'k': 'kiwi', 'j': 'jackfruit', 'l': 'leechi'}

In [85]:
print Cmy_fruits is my_fruits


False

In [86]:
# pop
# popitem
# clean

In [88]:
# pop
print help(Cmy_fruits.pop)
print Cmy_fruits.pop('a')


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
['apple', 'apple']

In [89]:
print Cmy_fruits


{'c': 'cherry', 'b': 'banana', 'd': 'dates', 'g': ['guava', 'grapes'], 'k': 'kiwi', 'j': 'jackfruit', 'l': 'leechi'}

In [91]:
# popitem
print help(Cmy_fruits.popitem)
print Cmy_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
('c', 'cherry')

In [92]:
print Cmy_fruits.popitem()


('b', 'banana')

In [93]:
# clear
print help(Cmy_fruits.clear)


Help on built-in function clear:

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

None

In [94]:
Cmy_fruits.clear()

In [96]:
print Cmy_fruits


{}

In [ ]:
# howework