In [ ]:
# Dictionaries
# hashes,hash,dict 
# ssn,UIN
# ABX123RTC : male,hyd,#301
# ABX1IRCTC : female,hyd,#301
# key : value
# key - unique
# value - repetative

In [1]:
my_fruits = {'a':'apple','b':'banana','c':'cherry','d':'dates'}
print my_fruits,type(my_fruits)


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

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


{} <type 'dict'>
{} <type 'dict'>

In [3]:
# how to retrive an element from a dictionary
print my_fruits['b']


banana

In [5]:
# modify
my_fruits['b']='brinjal'
print my_fruits


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

In [6]:
# adding a new key
my_fruits['f']='figs'
print my_fruits


{'a': 'apple', 'c': 'cherry', 'b': 'brinjal', 'd': 'dates', 'f': 'figs'}

In [10]:
my_fruits['a'] = ['apple','avacado']
print my_fruits


{'a': ['apple', 'avacado'], 'c': 'cherry', 'b': 'brinjal', 'd': 'dates', 'f': 'figs'}

In [8]:
# in operation
# in operation works only on key in a dictionary.
print 'a' in my_fruits
print 'apple' in my_fruits


True
False

In [26]:
# dictionary is an iterator
for key in my_fruits:
    print "key:{},value:{}".format(key,my_fruits[key])


key:a,value:['apple', 'avacado']
key:c,value:cherry
key:b,value:brinjal
key:d,value:dates
key:f,value:figs

In [ ]:
# task
# square of numbers from 1 to 10
# {1:1,2:4,.....,10:100}

In [11]:
my_square={}
num = int(raw_input("please enter the number:"))
for value in range(1,num+1):
    my_square[value]=value*value
print my_square


please enter the number:10
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

In [13]:
# functions

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 [ ]:
# fromkeys,setdefault,update => google -> tutorialpoint

In [14]:
# get
print help(my_fruits.get)


Help on built-in function get:

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

None

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


['apple', 'avacado']
['apple', 'avacado']

In [17]:
# has_key
print help(my_fruits.has_key)


Help on built-in function has_key:

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

None

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


True
True

In [19]:
# keys
print help(my_fruits.keys)


Help on built-in function keys:

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

None

In [20]:
print my_fruits.keys()


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

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


Help on built-in function iterkeys:

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

None

In [29]:
print my_fruits.iterkeys()
for value in my_fruits.iterkeys():
    print value


<dictionary-keyiterator object at 0x7f597831f158>
a
c
b
d
f

In [33]:
# viewkeys
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', 'f'])

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


Help on built-in function values:

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

None

In [37]:
print my_fruits.values()


[['apple', 'avacado'], 'cherry', 'brinjal', 'dates', 'figs']

In [31]:
# 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 0x7f597831f368>
['apple', 'avacado']
cherry
brinjal
dates
figs

In [ ]:
# viewvalues

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


[('a', ['apple', 'avacado']), ('c', 'cherry'), ('b', 'brinjal'), ('d', 'dates'), ('f', 'figs')]

In [32]:
# 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
<built-in method iteritems of dict object at 0x7f5978ba1280>
('a', ['apple', 'avacado'])
('c', 'cherry')
('b', 'brinjal')
('d', 'dates')
('f', 'figs')

In [ ]:
# viewitems

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


Help on built-in function copy:

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

None

In [14]:
# shallow copy works on complex objects

a = ['ant','ball','call','doll']
b = ['one','two','three','four']

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

# soft copy
Sc = Cc
print id(Sc),id(Cc)
print Sc is Cc
print Sc
print Cc
a[0]='Ant'
print Sc
print Cc

# deep copy
import copy
Dc = copy.deepcopy(Cc)
print id(Dc),id(Cc)
print Dc is Cc
print Dc
print id(Dc[0]),id(a),id(Dc[1]),id(b)
print Cc
b[0]="One"
print Dc
print Cc


[['ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
140599654901736 140599654902456 140599654902456 140599654902024 140599654902024
140599654901736 140599654901736
True
[['ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
[['ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
[['Ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
[['Ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
140599654560400 140599654901736
False
[['Ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
140599654828152 140599654902456 140599655346760 140599654902024
[['Ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
[['Ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
[['Ant', 'ball', 'call', 'doll'], ['One', 'two', 'three', 'four']]

In [15]:
# shallow 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 [19]:
a = ['ant','ball','call','doll']
b = ['one','two','three','four']

Cc = [a,b]
print Cc
Sh = copy.copy(Cc)
print Sh

print id(Cc),id(a),id(Cc[0]),id(b),id(Cc[1])
print id(Sh),id(a),id(Sh[0]),id(b),id(Sh[1])

a[1] = "ballon"
print Cc,Sh


[['ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
[['ant', 'ball', 'call', 'doll'], ['one', 'two', 'three', 'four']]
140599654900512 140599654900800 140599654900800 140599654900584 140599654900584
140599654561840 140599654900800 140599654900800 140599654900584 140599654900584
[['ant', 'ballon', 'call', 'doll'], ['one', 'two', 'three', 'four']] [['ant', 'ballon', 'call', 'doll'], ['one', 'two', 'three', 'four']]

In [20]:
D_fruits = my_fruits.copy()

In [22]:
print D_fruits
print my_fruits
print D_fruits is my_fruits


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

In [23]:
# pop
print help(my_fruits.pop)


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

In [24]:
D_fruits.pop('d')


Out[24]:
'dates'

In [25]:
print D_fruits


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

In [26]:
# popitem
print help(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

In [27]:
print D_fruits.popitem()


('a', 'apple')

In [28]:
print D_fruits.popitem()


('c', 'cherry')

In [29]:
print D_fruits


{'b': 'banana'}

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


Help on built-in function clear:

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

None

In [32]:
D_fruits.clear()

In [33]:
print D_fruits


{}

In [ ]:
# 
# numpy,pandas
# http://tinyurl.com/TconSeminars