We can use List, Tuple, and Set to represent a group of individual objects as a single entity.
If we want to represent a group of objects as key-value pairs then we should go for Dictionary.
Eg:
rollno----name phone number--address ipaddress---domain name
Duplicate keys are not allowed but values can be duplicated.
Heterogeneous objects are allowed for both keys and values.
insertion order is not preserved
Dictionaries are mutable
Dictionaries are dynamic
indexing and slicing concepts are not applicable
How to create Dictionary?
d={} or d=dict()
we are creating an empty dictionary. We can add entries as follows
d[100]="Durga"
d[200]="Ravi"
d[300]="shiva"
print(d) #{100: 'Durga', 200: 'Ravi', 300: 'shiva'}
If we know data in advance then we can create a dictionary as follows
d={100:'durga' ,200:'ravi', 300:'shiva'}
d={key:value, key:value}
How to access data from the dictionary?
We can access data by using keys.
d={100:'durga' ,200:'ravi', 300:'shiva'}
print(d[100]) #durga
print(d[300]) #shiva
If the specified key is not available then we will get KeyError
print(d[400]) # KeyError: 400
We can prevent this by checking whether key is already available or not by using
has_key() function or by using in operator.
d.has_key(400) ==> returns 1 if key is available otherwise returns 0
But has_key() function is available only in Python 2 but not in Python 3. Hence
compulsory we have to use an the operator.
if 400 in d:
print(d[400])
How to update dictionaries?
d[key]=value
If the key is not available then a new entry will be added to the dictionary with the specified key-value pair
If the key is already available then old value will be replaced with new value.
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. print(d) 3. d[400]="pavan" 4. print(d) 5. d[100]="sunny" 6. print(d) 7. 8. Output 9. {100: 'durga', 200: 'ravi', 300: 'shiva'} 10. {100: 'durga', 200: 'ravi', 300: 'shiva', 400: 'pavan'} 11. {100: 'sunny', 200: 'ravi', 300: 'shiva', 400: 'pavan'}
How to delete elements from the dictionary?
del d[key]
It deletes entries associated with the specified key.
If the key is not available then we will get KeyError
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. print(d) 3. del d[100] 4. print(d) 5. del d[400] 6. 7. Output 8. {100: 'durga', 200: 'ravi', 300: 'shiva'} 9. {200: 'ravi', 300: 'shiva'} 10. KeyError: 400
d.clear()
To remove all entries from the dictionary
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. print(d) 3. d.clear() 4. print(d) 5. 6. Output 7. {100: 'durga', 200: 'ravi', 300: 'shiva'} 8. {}
del d
To delete the total dictionary. Now we cannot access d
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. print(d) 3. del d 4. print(d) 5. 6. Output 7. {100: 'durga', 200: 'ravi', 300: 'shiva'} 8. NameError: name 'd' is not defined
Important functions of dictionary:
1. dict():
To create a dictionary
d=dict() ===>It creates empty dictionary
d=dict({100:"durga",200:"ravi"}) ==>It creates dictionary with specified elements
d=dict([(100,"durga"),(200,"shiva"),(300,"ravi")])==>It creates dictionary with the given list of tuple elements
2. len()
Returns the number of items in the dictionary
3. clear():
To remove all elements from the dictionary
4. get():
To get the value associated with the key
d.get(key)
If the key is available then returns the corresponding value otherwise returns None. It wont raise any error.
d.get(key,defaultvalue)
If the key is available then returns the corresponding value otherwise returns default value.
Eg:
d={100:"durga",200:"ravi",300:"shiva"} print(d[100]) ==>durga print(d[400]) ==>KeyError:400 print(d.get(100)) ==durga print(d.get(400)) ==>None print(d.get(100,"Guest")) ==durga print(d.get(400,"Guest")) ==>Guest
3. pop():
d.pop(key)
It removes the entry associated with the specified key and returns the corresponding value
If the specified key is not available then we will get KeyError
Eg:
1) d={100:"durga",200:"ravi",300:"shiva"} 2) print(d.pop(100)) 3) print(d) 4) print(d.pop(400)) 5) 6) Output 7) durga 8) {200: 'ravi', 300: 'shiva'} 9) KeyError: 400
4. popitem():
It removes an arbitrary item(key-value) from the dictionaty and returns it.
Eg:
1) d={100:"durga",200:"ravi",300:"shiva"} 2) print(d) 3) print(d.popitem()) 4) print(d) 5) 6) Output 7) {100: 'durga', 200: 'ravi', 300: 'shiva'} 8) (300, 'shiva') 9) {100: 'durga', 200: 'ravi'}
If the dictionary is empty then we will get KeyError
d={}
print(d.popitem()) ==>KeyError: 'popitem(): dictionary is empty'
5. keys():
It returns all keys associated eith dictionary
Eg:
1) d={100:"durga",200:"ravi",300:"shiva"} 2) print(d.keys()) 3) for k in d.keys(): 4) print(k) 5) 6) Output 7) dict_keys([100, 200, 300]) 8) 100 9) 200 10) 300
6. values():
It returns all values associated with the dictionary
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. print(d.values()) 3. for v in d.values(): 4. print(v) 5. 6. Output 7. dict_values(['durga', 'ravi', 'shiva']) 8. durga 9. ravi 10. shiva
7. items():
It returns list of tuples representing key-value pairs.
[(k,v),(k,v),(k,v)]
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. for k,v in d.items(): 3. print(k,"--",v) 4. 5. Output 6. 100 -- durga 7. 200 -- ravi 8. 300 -- shiva
8. copy():
To create exactly duplicate dictionary(cloned copy)
d1=d.copy();
9. setdefault():
d.setdefault(k,v)
If the key is already available then this function returns the corresponding value.
If the key is not available then the specified key-value will be added as new item to the dictionary.
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"} 2. print(d.setdefault(400,"pavan")) 3. print(d) 4. print(d.setdefault(100,"sachin")) 5. print(d) 6. 7. Output 8. pavan 9. {100: 'durga', 200: 'ravi', 300: 'shiva', 400: 'pavan'} 10. durga 11. {100: 'durga', 200: 'ravi', 300: 'shiva', 400: 'pavan'}
Dictionary Comprehension:
The comprehension concept is applicable to dictionaries also.
1. squares={x:x*x for x in range(1,6)} 2. print(squares) 3. doubles={x:2*x for x in range(1,6)} 4. print(doubles) 5. 6. Output 7. {1: 1, 2: 4, 3: 9, 4: 16, 5: 25} 8. {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
"Python Dictionary Data
Structure"
"Python Dictionary Data Structure Implementation"
"Python Dictionary Underlying Data
Structure"
"Python Sorted Dictionary Data Structure"
"Dictionary In Python Is A Mutable Data Structure"
"Python Data Structures List Dictionary"
"Python 3 Dictionary Data
Structure"