Dictionaries


In [25]:
# create an empty dictionary
d = dict()
# or
d = {}

In [26]:
bob1 = dict(name='Bob', job='dev', age=40)
print bob1


{'age': 40, 'job': 'dev', 'name': 'Bob'}

In [27]:
bob2 = dict(zip(['name', 'job', 'age'], ['Bob', 'dev', 40]))
print bob2


{'age': 40, 'job': 'dev', 'name': 'Bob'}

In [31]:
bob2['name'] = 'jimmy'

In [32]:
bob2['name']


Out[32]:
'jimmy'

In [33]:
bob1['name']


Out[33]:
'Bob'

In [29]:
# create a dictionary
d = {'Name': 'Jimmy', 
     "phone": '850-882-0340', 
     "Work": "Eglin AFB",
     1:3}
d


Out[29]:
{1: 3, 'Name': 'Jimmy', 'Work': 'Eglin AFB', 'phone': '850-882-0340'}

In [81]:
# get the value of 'Name'
d['Name']


Out[81]:
'Jimmy'

In [45]:
# dictionary keys are case sensitive
d['name']


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-45-5af8309a2bc6> in <module>()
      1 # dictionary keys are case sensitive
----> 2 d['name']

KeyError: 'name'

In [83]:
# get value of a key. it not present give back message
d.get('name', 'no such key')


Out[83]:
'no such key'

In [46]:
# change a value
d['Name'] = 'James'
d


Out[46]:
{1: 3, 'Name': 'James', 'Work': 'Eglin AFB', 'phone': '850-882-0340'}

In [47]:
# add a key-value
d['school'] = 'Aubun'
d


Out[47]:
{1: 3,
 'Name': 'James',
 'Work': 'Eglin AFB',
 'phone': '850-882-0340',
 'school': 'Aubun'}

In [48]:
# rm a key-value
del d['school']
d


Out[48]:
{1: 3, 'Name': 'James', 'Work': 'Eglin AFB', 'phone': '850-882-0340'}

In [49]:
# change it to a string
str(d)


Out[49]:
"{'phone': '850-882-0340', 'Work': 'Eglin AFB', 'Name': 'James', 1: 3}"

In [50]:
# get its length
len(d)


Out[50]:
4

In [51]:
# clear dictionary but don't delete it
d.clear()
d


Out[51]:
{}

In [52]:
# create a dictionary
d = {'Name': 'Jimmy', 
     "phone": '850-882-0340', 
     "Work": "Eglin AFB"}

In [53]:
# check for key
d.has_key('name')


Out[53]:
False

In [54]:
# get the items of a dictionary
d.items()


Out[54]:
[('phone', '850-882-0340'), ('Work', 'Eglin AFB'), ('Name', 'Jimmy')]

In [55]:
# just get the keys
d.keys()


Out[55]:
['phone', 'Work', 'Name']

In [56]:
# just get the values
d.values()


Out[56]:
['850-882-0340', 'Eglin AFB', 'Jimmy']

In [57]:
# print both the keys and values
for k,v in d.items():
    print k,v


phone 850-882-0340
Work Eglin AFB
Name Jimmy

In [58]:
# copy a dictionay
dd = d
print dd is d
print d,dd


True
{'phone': '850-882-0340', 'Work': 'Eglin AFB', 'Name': 'Jimmy'} {'phone': '850-882-0340', 'Work': 'Eglin AFB', 'Name': 'Jimmy'}

In [59]:
dd['Name']='Alex'

In [126]:
d,dd


Out[126]:
({'Name': 'Alex', 'Work': 'Eglin AFB', 'phone': '850-882-0340'},
 {'Name': 'Alex', 'Work': 'Eglin AFB', 'phone': '850-882-0340'})

In [60]:
# change a value
d['Name'] = 'JT'
d,dd


Out[60]:
({'Name': 'JT', 'Work': 'Eglin AFB', 'phone': '850-882-0340'},
 {'Name': 'JT', 'Work': 'Eglin AFB', 'phone': '850-882-0340'})

In [128]:
dd is d


Out[128]:
True

In [61]:
# another way to copy dictionaries
dd = d.copy()
print dd is d
print d,dd


False
{'phone': '850-882-0340', 'Work': 'Eglin AFB', 'Name': 'JT'} {'phone': '850-882-0340', 'Work': 'Eglin AFB', 'Name': 'JT'}

In [62]:
# change a value
dd['Name']='Alex'
d,dd


Out[62]:
({'Name': 'JT', 'Work': 'Eglin AFB', 'phone': '850-882-0340'},
 {'Name': 'Alex', 'Work': 'Eglin AFB', 'phone': '850-882-0340'})

In [63]:
d['Name'] = 'JT'
d,dd


Out[63]:
({'Name': 'JT', 'Work': 'Eglin AFB', 'phone': '850-882-0340'},
 {'Name': 'Alex', 'Work': 'Eglin AFB', 'phone': '850-882-0340'})

In [34]:
# nesting - no restrictions on value
rec = {'name': {'first': ['bob', 'mob'], 'last': 'Smith'},
           'jobs': ['dev', 'mgr'],
           'age':  40.5}
print rec


{'age': 40.5, 'jobs': ['dev', 'mgr'], 'name': {'last': 'Smith', 'first': ['bob', 'mob']}}

In [35]:
rec.keys()


Out[35]:
['age', 'jobs', 'name']

In [36]:
rec.values()


Out[36]:
[40.5, ['dev', 'mgr'], {'first': ['bob', 'mob'], 'last': 'Smith'}]

In [37]:
# access the first element
rec['name']


Out[37]:
{'first': ['bob', 'mob'], 'last': 'Smith'}

In [38]:
rec['name']['first']


Out[38]:
['bob', 'mob']

In [40]:
rec['name']['first'][1]


Out[40]:
'mob'