Tuesday, 24 January 2017

DICTIONARY IN PYTHON

Dictionary is used to represent group of key, value pairs into a single entity. Insertion order is not preserved. Duplicate keys are not allowed but duplicate values can be there. Heterogeneous keys and values are allowed. Immutable objects only allowed for keys.

Example –
x={}
print x
print type(x)
y=dict([(1,'apple'),(2,'ball')])
print y
print type(y)
z={'java':90,'Python':99,'hadoop':95}
print z
print type(z)
print len(z)

Output –
{}
<type 'dict'>
{1: 'apple', 2: 'ball'}
<type 'dict'>
{'Python': 99, 'java': 90, 'hadoop': 95}
<type 'dict'>
3


Example –
x={10:'abc',20:'def',30:'xyz',20:'rakesh',40:'ghi'}
print x
print x[10]
print x[20]
print x[50]
Output –
Traceback (most recent call last):
{40: 'ghi', 10: 'abc', 20: 'rakesh', 30: 'xyz'}
abc
  File "C:/Users/om/PycharmProjects/Pythonn/if3.py", line 5, in <module>
rakesh
    print x[50]
KeyError: 50


Example –
x={10:12.12,True:100,1.1:'abc','def':False,'names':['hyd','bang','ghy'],
(1.1,2.2,3.3):'india'}
print x
k=x.keys()
print k
v=x.values()
print v

Output –
{True: 100, 1.1: 'abc', 10: 12.12, (1.1, 2.2, 3.3): 'india', 'names': ['hyd', 'bang', 'ghy'], 'def': False}
[True, 1.1, 10, (1.1, 2.2, 3.3), 'names', 'def']
[100, 'abc', 12.12, 'india', ['hyd', 'bang', 'ghy'], False]


Example –
x={[10,20,30]:'rakesh'}
print x
y={{'abc','def','xyz'}:'apurba'}
print y
z={(10,20,[100,200]):'ghy'}
print z

Output –
Traceback (most recent call last):
  File "C:/Users/om/PycharmProjects/Pythonn/if3.py", line 1, in <module>
    x={[10,20,30]:'rakesh'}
TypeError: unhashable type: 'list'


Example –
x={10:'abc',20:'def',30:'ijk',40:'def'}
print x
x[50]='hyd'
print x
x[40]='ghy'
print x
x.pop(30)
print x
x.popitem()
print x
del x[20]
print x
x.clear()
print x
del x

Output –
{40: 'def', 10: 'abc', 20: 'def', 30: 'ijk'}
{40: 'def', 10: 'abc', 20: 'def', 50: 'hyd', 30: 'ijk'}
{40: 'ghy', 10: 'abc', 20: 'def', 50: 'hyd', 30: 'ijk'}
{40: 'ghy', 10: 'abc', 20: 'def', 50: 'hyd'}
{10: 'abc', 20: 'def', 50: 'hyd'}
{10: 'abc', 50: 'hyd'}
{}


Example –
x={'barsha':60,'bikash':65,'apurba':58,'rimi':63}
print x
y=x.items()
print y
for p in y:
    print p[0],'\t',p[1]
Output –
{'bikash': 65, 'barsha': 60, 'rimi': 63, 'apurba': 58}
[('bikash', 65), ('barsha', 60), ('rimi', 63), ('apurba', 58)]
bikash        65
barsha        60
rimi             63
apurba       58


PROGRAM FOR ADDITION OF TWO 3X3 MATRIX
x=[[12,7,3],[4,5,6],[7,8,9]]
y=[[5,8,1],[6,7,3],[4,5,6]]
result=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(x)):
    for j in range(len(x[0])):
        result[i][j]=x[i][j]+y[i][j]
for r in result:
    print r    
    
Output –
[17, 15, 4]
[10, 12, 9]
[11, 13, 15]


Example –
marks={}.fromkeys(['math','computer','java'],0)
print marks
for item in marks.items():
    print item
print sorted(marks.keys())

Output –
{'computer': 0, 'java': 0, 'math': 0}
('computer', 0)
('java', 0)
('math', 0)
['computer', 'java', 'math']


Example –
squares = {x*x*x for x in range(6)}
print squares
odd_square = {x:x*x for x in range(11) if x%2==1}
print odd_square
d={n:True for n in range(5)}
print d

Output –
set([0, 1, 8, 64, 27, 125])
{1: 1, 3: 9, 9: 81, 5: 25, 7: 49}
{0: True, 1: True, 2: True, 3: True, 4: True}


No comments:

Post a Comment