Dictionary


In [4]:
dictionary = {'key1':'val1' , 'key2':'val2' , 'key3':'val3'}
print 'Value at key1 is: {x}'.format(x = dictionary['key1'])


Value at key1 is: val1

In [17]:
myDictionary = {}
myDictionary['k1'] = 10
myDictionary['k2'] = 15.3
myDictionary['k3'] = 'Hello'
myDictionary['k4'] = [1,2,3]
print 'Value at k1 is: {x}'.format(x = myDictionary['k1'])
print 'First character of value at key k3 is : {x}'.format(x = myDictionary['k3'][0])


Value at k1 is: 10
First character of value at key k3 is : H

In [11]:
print 'content of My dictionary is: {x}'.format(x = myDictionary.items())


content of My dictionary is: [('k3', 'Hello'), ('k2', 15.3), ('k1', 10), ('k4', [1, 2, 3])]

In [18]:
print 'Keys  of My disctioary is: {x}'.format(x = myDictionary.keys())
print 'Values  of My disctioary is: {x}'.format(x = myDictionary.values())


Keys  of My disctioary is: ['k3', 'k2', 'k1', 'k4']
Values  of My disctioary is: ['Hello', 15.3, 10, [1, 2, 3]]

In [13]:
#remove element from disctionary
myDictionary = {'k1':10, 'k2':15.3, 'k3':'Hello', 'k4': [1,2,3]}
myDictionary.pop('k1')
print 'my dictionary after removing element at key k1 is: {x}'.format(x = myDictionary)


my dictionary after removing element at key k1 is: {'k3': 'Hello', 'k2': 15.3, 'k4': [1, 2, 3]}

In [ ]: