Dictionary
- Dictionary is an unordered set of key and value pair.
- It is an container that contains data, enclosed within curly braces.
- The key and the value is separated by a colon(:). This pair is known as item.
- Items are separated from each other by a comma(,).
- Dictionary is mutable i.e., value can be updated.
- Key must be unique and immutable. Value is accessed by key. Value can be updated while key cannot be changed.
- Dictionary is known as Associative array.
Example: -
stud={1:’Reena’, 2:’Seema’, 3:’Rahul’}
print stud
Accessing Values:-
As index is not defined, a Dictionaries value can be accessed by their keys.
Updation:-
New item can be added. The values can be changed.
Dictionary Operations:-
- get()
- del()
- cmp()
- keys()
- values()
- items()
- clear()
- copy()
- update()
- has_key()
- from_keys()
Dictionary: Operations: get(key):-
get(key):-
Returns the value of the given key. If key is not present it returns none.
Syntax:-
dict.get(key, default = None)
The get() method takes maximum of two parameters:
key - key to be searched in the dictionary value (optional) - Value to be returned if the key is not found. The default value is None.
Return Value from get() The get() method returns:-
- The value for the specified key if key is in dictionary.
- None if the key is not found and value is not specified.
- value if the key is not found and value is specified.
Example:-
person = {'name': 'Ramesh', 'age': 22}
print('Name: ', person.get('name'))
print('Age: ', person.get('age'))
# value is not provided
print('Salary: ', person.get('salary'))
# value is provided
print('Salary: ', person.get('salary', 0.0))
Name: Ramesh
Age: 22
Salary: None
Salary: 0.0
Dictionary: Operations: del():-
- Deletion: An item can be deleted using del statement from a dictionary using the key. Whole dictionary can also be deleted using del statement.
Syntax:-
del [key]
Example:-
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in
dict del dict ; # delete entire dictionary
Dictionary: Operations: cmp():-
- cmp():-
compares two dictionaries based on key and values.
Syntax:-
cmp(dict1, dict2)
returns
- 0 if both dictionaries are equal,
- -1 if dict1 < dict2 and
- 1 if dict1 > dic2.
dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = {'Name': 'Mahnaz', 'Age': 27};
dict3 = {'Name': 'Abid', 'Age': 27};
dict4 = {'Name': 'Zara', 'Age': 7};
print "Return Value : %d" % cmp (dict1, dict2) # -1
print "Return Value : %d" % cmp (dict2, dict3) #1
print "Return Value : %d" % cmp (dict1, dict4 #0
Dictionary: Operations: keys()
keys():- Return all the keys element of a dictionary.
Syntax:-
dict.keys()
Example:-
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.keys()
Output:-
Value : ['Age', 'Name']
Dictionary: Operations: values()
values():- Return all the values element of a dictionary.
Syntax:-
dict.values()
Example:-
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.values()
Output:-
Value : ['Zara', 7]
Dictionary: Operations: items()
items():-Return all the items(key-value pair) of a dictionary.
Syntax:-
dict.items()
Example:-
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.values()
Output:-
Value : [('Age', 7), ('Name', 'Zara')]
Dictionary: Operations: clear()
clear():- It is used to remove all items of a dictionary. It returns an empty dictionary.
Syntax:-
dict.clear()
Example:-
dict = {'Name': 'Zara', 'Age': 7};
print "Len : %d" % len(dict)
dict.clear()
print “Len : %d" % len(dict)
Output:-
Len : 2 Len : 0
Dictionary: Operations: copy()
copy():- It returns an ordered copy of the data.
Syntax:-
dict.copy()
Example:-
dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = dict1.copy()
print "New Dictionary : %s" % str(dict2)
Output:-
New Dictionary : {'Age': 7, 'Name': 'Zara'}
Dictionary: Operations: update()
update(dictionary2): It is used to add items of dictionary2 to first dictionary.
Syntax:-
dict.update(dict2)
Example:-
dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female' }
dict.update(dict2)
print "Value : %s" % dict
Output:-
Value : { 'Name': 'Zara', 'Age': 7, 'Sex': 'female'}
Dictionary: Operations: has_key()
has_key(key):- It returns a boolean value. True in case if key is present in the dictionary ,else false.
Syntax:-
dict.has_key(key)
Example:-
dict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % dict.has_key('Age')
print "Value : %s" % dict.has_key('Sex')
Output:-
Value : True Value : False
Dictionary: Operations: fromkeys()
fromkeys(sequence,value1):- creates a new dictionary with keys from seq and values set to value.
Syntax:-
dict.fromkeys(seq[, value])
seq − list of values which would be used for dictionary keys preparation.
value − Optional, if provided then value would be set to this value.
Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)
Check Out Our Python ALL Post In Just One Click....
Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)


1 Comments
I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyDeleteWeb Designing Course in chennai
Web Designing training in chennai
Big Data Analytics Courses in Chennai
Hadoop Course in Chennai
Big Data Course in Chennai
Selenium Training in Chennai
German Language Course in Chennai
Web designing Training in Porur