In [ ]:
# Seminar Videos : http://tinyurl.com/TconSeminars
In [ ]:
# dictionaries
# hashes
# dict
# key(unique) => value(duplicated)
# ssn
# uin
In [1]:
# define your dictionary.
students = {'ramesh':'python','pradeep':'python','prakash':'ruby'}
print students
In [4]:
my_empty = {}
print my_empty,type(my_empty)
In [5]:
my_empty = dict()
print my_empty,type(my_empty)
In [6]:
# cheatsheet
# list => ['apple','banana'],[],list()
# tuples => ('apple','banana'),(),tuple()
# dictionaries => {'a':'apple','b':'ball'},{},dict()
In [7]:
# access my dictionary
print students['ramesh']
In [8]:
# Adding a new key and value.
students['kumar'] = "aws"
print students
In [9]:
# replace
students['kumar'] = 'openstack'
print students
In [10]:
# A key having multiple values.
students['kumar'] = ['openstack','aws']
print students
In [12]:
# in operator
# in operator is true for the key values.
print 'prakash' in students
print 'ruby' in students
In [13]:
# function
print dir(students)
In [14]:
# get
print help(students.get)
In [19]:
print students.get('kumar')
print students['kumar']
print students.get('hari')
print students['hari']
In [21]:
# has_key
print help(students.has_key)
In [22]:
print students.has_key('kumar')
print students.has_key('hari')
In [23]:
print 'kumar' in students
print 'hari' in students
In [24]:
# update
print help(students.update)
In [25]:
print students
In [30]:
fruits = {'a':'apple','b':'banana'}
print fruits
In [31]:
students.update(fruits)
print students
In [32]:
# fromkeys
print help(students.fromkeys)
In [34]:
names = ('hari','dinesh','kumar')
print students.fromkeys(names,"python")
print students.fromkeys(names)
In [35]:
# keys,values,items
# keys
print help(students.keys)
print students.keys()
In [42]:
# iterkeys
print help(students.iterkeys)
print students.iterkeys
print students.iterkeys()
for value in students.iterkeys():
print value
In [44]:
# viewkeys
print help(students.viewkeys)
print students.viewkeys()
In [36]:
# values
# itervalues
# viewvalues
print help(students.values)
print students.values()
In [37]:
# items
# iteritems
# viewitems
print help(students.items)
print students.items()
In [47]:
# pop
print help(students.pop)
students.pop('a')
print students
students.pop('a')
In [51]:
# popitem
print help(students.popitem)
print students.popitem()
print students.popitem()
In [ ]:
# tasks
# zip
# del
In [52]:
# setdefault
print help(students.setdefault)
In [53]:
students.setdefault('hari','python')
Out[53]:
In [54]:
print students
In [55]:
print students.setdefault('hari','django')
In [56]:
print students
In [57]:
print students.setdefault('kumar','python')
print students
In [58]:
students['hari']='django'
print students
In [ ]:
# tomorrow
# clear
# copy - shallowcopy
In [ ]: