Dictionary📘
Dictionaries are wonderful data structure available in python.
Most of the elements/Building Blocks are dictionary in python.
Defined as An associative array or collection, where arbitrary keys are mapped to values.
Keys are much like a set. Keys are real keys for Dictionary manipulation.
Dictionary maps keys to values. It is Ordered data structure.
Dictionary is iterable. Supports access by integer indices.
#Creating
dict = {'slNo': 1,
'name':'Ram',
'sal':1200000
}
# Accessing by Key
print(dict['slNo'])
print(dict['name'])
print(dict['sal'])
Result:
1Ram
In thee above see the dictionary creation with keys as slNo, name, sal. You can have anything as a key.
You can access by key as show n above. For eg dict['name'].
# print only keys
for key in dict:
print(key)
Result:
slNoname
# print only Values
for key in dict:
print(dict[key])
Result:
Ram
1200000
#print the whole dictionary
print(dict)
Result:
{'slNo': 1, 'name': 'Ram', 'sal': 1200000}
items = dict.items()
print(items)
Result:
dict_items([('slNo', 1), ('name', 'Ram'), ('sal', 1200000)])
Print individual key, value pair by iteration since it is iterable.
#iterating by .items()
for item in dict.items():
print(item)
Result:
('slNo', 1)('name', 'Ram')
('sal', 1200000).
Please note this with curly brackets. '()'
########### Printing key value pair as tuple...Unpacking
for key, value in dict.items():
print(key,':',value)
############
Result:
slNo : 1name : Ram
Modifying the value for key 'slNo'
##Modifying value
dict['slNo'] = 999
print(dict)
Result:
{'slNo': 999, 'name': 'Ram', 'sal': 1200000}
Deleting a Key. Please note clear() function will delete the whole dictionary
#remove a single key and value
del dict['slNo']
print(dict)
Result:
{'name': 'Ram', 'sal': 1200000}
Please you try this piece of code at the end of this tutorial.
dict.clear();
print(dict,type(dict))
Result:
{} <class 'dict'>
Adding a new Key 'SLNO', value 12345 pair
print(dict)
dict['SLNO'] = 12345
print(dict)
Result:
{'slNo': 1, 'name': 'Ram', 'sal': 1200000}{'slNo': 1, 'name': 'Ram', 'sal': 1200000, 'SLNO': 12345}
Let us have a break. We play with some funny example. In the above dictionary, can you try to transmit key as values and values as keys. Yeah.. You can do it with this piece of code, man!! with comprehensive iteration as show with for..loop.....
#Brain stormer Comprehensive to switch v->k, k->v
print(dict)
switch_key_value = {value: key for key, value in dict.items()}
print(switch_key_value)
Result:
{'slNo': 1, 'name': 'Ram', 'sal': 1200000}
{1: 'slNo', 'Ram': 'name', 1200000: 'sal'}
Sorting: Super easy in dictionary by keys or values.
Creating dictionary by keys for sorting() sample
dicts = {'apple':10000, 'orange':7000, 'bananna': 12000 }
for k in sorted(dicts):
print(k,'=',dicts[k])
print(30*'#','Sorted by Keys')
Result: (sorted by Keys)
apple = 10000
bananna = 12000
orange = 7000
#Function to return values
def by_value(item):
return item[1]
print(10*'#', 'sorting dictionary by values#')
#Iteartion for sorting the dictionary by values
for k, v in sorted(dicts.items(), key = by_value):
print(k,'=',dicts[k])
print(10*'#', 'with out bothering keys sorting only by values#')
for value in sorted(dicts.values()):
print(value)
Please note a function is defined to return values.
########## sorting dictionary by values#
orange = 7000
apple = 10000
bananas = 12000
#### with out bothering keys sorting only by values#
7000
10000
12000
Hope you learned some of the very basics of python Dictionary. Happy Learning.😊😊😊😊
No comments:
Post a Comment