Chp-11: Dictionaries#
Chapter Objectives
By the end of this chapter, the student should be able to:
Explain the purpose and role of dictionaries.
Recognize the key-value pair structure of dictionaries.
Create and initialize dictionaries in different ways.
Implement dictionaries to efficiently organize and access data.
Apply dictionary methods.
Use loops to iterate through dictionary keys, values, and pairs.
Use dictionary comprehensions.
Apply dictionaries to solve real-world problems.
Dictionaries#
These are a more general form of lists. The indexes of lists are integers starting from 0, whereas the indexes, called keys in dictionaries, can be chosen from different types.
Its elements are pairs of the form
key:value
.Dictionaries are created by using curly brackets, like sets, by using
key:value
pairs and:
in between.Keys are like indexes, and values can be accessed by using square brackets in the form of
dictionary_name[key]
.Keys must be immutable, like strings, numbers, and tuples.
A list cannot be a key of a dictionary.
Values can be any type, including strings, numbers, booleans, tuples, lists, and dictionaries.
Dictionary pairs are ordered.
If there are two pairs with the same key, then the later pair will overwrite the former one.
Dictionaries are mutable, so they can be modified.
Since dictionaries can be modified, they have a large number of methods.
The
len()
function returns the number of pairs.The built-in
dict()
function can be used to create dictionaries from structures that have pairs.{}
is an empty dictionary.An empty set is
set()
.
Create Dictionaries#
# empty dictionary
empty_dict = {}
print(f'Empty dictionary : {empty_dict}')
print(f'Type of empty_dict: {type(empty_dict)}')
Empty dictionary : {}
Type of empty_dict: <class 'dict'>
# empty dictionary with dict()
empty_dict = dict()
print(f'Empty dictionary : {empty_dict}')
print(f'Type of empty_dict: {type(empty_dict)}')
Empty dictionary : {}
Type of empty_dict: <class 'dict'>
# three pairs
# keys: 'Math', 'Chemistry', 'History'
# values: 80, 70, 65
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(grades)
{'Math': 80, 'Chemistry': 70, 'History': 65}
# overwrite first pair of math
grades = {'Math':80, 'Chemistry':70, 'History':65, 'Math':100}
print(grades) # The value of 'Math' key is 100
{'Math': 100, 'Chemistry': 70, 'History': 65}
# values can be lists, tuples
grades = {'Math':[80, 90], 'Chemistry':(70, 100), 'History':65}
print(grades)
{'Math': [80, 90], 'Chemistry': (70, 100), 'History': 65}
# lists can not be a key
grades = {['Math', 'Biology']:80, 'Chemistry':70, 'History':65} # ERROR
dict()#
The built-in dict() function converts tuples, and lists consists of pairs into a dictionary.
# assignments ----> dict
grades = dict(Math=80, Chemistry=70, History=65)
print(grades)
{'Math': 80, 'Chemistry': 70, 'History': 65}
# tuples of pairs ----> dict
grades_tuple = ( ('Math', 80), ('Chemistry', 70), ('History', 6))
grades = dict(grades_tuple)
print(grades)
{'Math': 80, 'Chemistry': 70, 'History': 6}
# lists of pairs ----> dict
grades_list = [ ['Math', 80], ['Chemistry', 70], ['History', 6]]
grades = dict(grades_list)
print(grades)
{'Math': 80, 'Chemistry': 70, 'History': 6}
len()#
The len() function returns the number of pairs in a dictionary.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'The number of pairs: {len(grades)}')
The number of pairs: 3
in & not in#
in
tests whether a given term is a key of a dictionary.not in
tests whether a given term is not a key of a dictionary.Both of them return boolean values, True or False.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f' Math is a key of grades: {"Math" in grades}' ) # use " instead of ' in f-strings
print(f' Math is not a key of grades: {"Math" not in grades}' )
Math is a key of grades: True
Math is not a key of grades: False
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f' Biology is a key of grades: {"Biology" in grades}' ) # use " instead of ' in f-strings
print(f' Biology is not a key of grades: {"Biology" not in grades}' )
Biology is a key of grades: False
Biology is not a key of grades: True
Mutable#
Unlike strings and tuples, and like lists, dictionaries are mutable, which means they can be modified.
New pairs can be added, and existing pairs can be modified.
Add a new pair#
Use square brackets in the form of
dictionary_name[new_key] = new_value
.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary before adding a new pair: {grades}')
# add the pair 'Eglish':99
grades['English'] = 99
print(f'grades dictionary after adding a new pair: {grades}')
grades dictionary before adding a new pair: {'Math': 80, 'Chemistry': 70, 'History': 65}
grades dictionary after adding a new pair: {'Math': 80, 'Chemistry': 70, 'History': 65, 'English': 99}
Change a Value#
Use square brackets in the form of
dictionary_name[key] = new_value
.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary before changing a value: {grades}')
# change the value of 'Chemistry'
grades['Chemistry'] = 85
print(f'grades dictionary after adding a new pair: {grades}')
grades dictionary before changing a value: {'Math': 80, 'Chemistry': 70, 'History': 65}
grades dictionary after adding a new pair: {'Math': 80, 'Chemistry': 85, 'History': 65}
Delete a pair#
Use
del
in the form ofdel dictionary_name[key]
.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary before deleting a value: {grades}')
# delete 'Chemistry':70 pair
del grades['Chemistry']
print(f'grades dictionary after deleting a pair: {grades}')
grades dictionary before deleting a value: {'Math': 80, 'Chemistry': 70, 'History': 65}
grades dictionary after deleting a pair: {'Math': 80, 'History': 65}
Dictionary Methods#
Except for the magic methods (the ones with underscores), there are 11 methods for dictionaries.
You can run help(dict) for more details.
# methods of dictionaries
# dir() returns a list
print(dir(dict))
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
# non magic methods by using slicing
print(dir(dict)[-11:])
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
clear()#
It removes all pairs from the dictionary, making it an empty dictionary.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary before using clear(): {grades}')
# remove all elements of grades dictionary
grades.clear()
print(f'grades dictionary after using clear(): {grades}')
grades dictionary before using clear(): {'Math': 80, 'Chemistry': 70, 'History': 65}
grades dictionary after using clear(): {}
copy()#
It returns a new dictionary with the same pairs.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary : {grades}')
# copy all elements of grades dictionary
grades_copy = grades.copy()
print(f'grades_copy dictionary: {grades}')
grades dictionary : {'Math': 80, 'Chemistry': 70, 'History': 65}
grades_copy dictionary: {'Math': 80, 'Chemistry': 70, 'History': 65}
get()#
It takes a key and a default value.
If the given key exists in the dictionary, get() returns the corresponding value; otherwise, it returns the default value.
Its purpose is to prevent errors for non-existing key-value pairs.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary : {grades}')
# value corresponding to 'Math' key
val = grades.get('Math', 'Does Not Exist') # Math key exist so get returns 80 not 'Does Not Exist'
print(f'The value of Math key: {val}')
grades dictionary : {'Math': 80, 'Chemistry': 70, 'History': 65}
The value of Math key: 80
In the following code, since the key ‘Biology’ does not exist get() returns the default value ‘Does Not Exist’.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary : {grades}')
# value corresponding to the key 'Biology'
val = grades.get('Biology', 'Does Not Exist')
print(f'The value of Biology key: {val}')
grades dictionary : {'Math': 80, 'Chemistry': 70, 'History': 65}
The value of Biology key: Does Not Exist
items()#
It returns the pairs of a dictionary as a tuple in a data structure called dict_items.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'Items: {grades.items()}')
print(f'Type : {type(grades.items())}')
Items: dict_items([('Math', 80), ('Chemistry', 70), ('History', 65)])
Type : <class 'dict_items'>
keys()#
It returns the keys of a dictionary in a data structure called dict_keys.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'Keys: {grades.keys()}')
print(f'Type: {type(grades.keys())}')
Keys: dict_keys(['Math', 'Chemistry', 'History'])
Type: <class 'dict_keys'>
pop()#
It removes a key-value pair for a given key and returns the value removed.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'grades dictionary before using pop(): {grades}')
# remove the pair 'Chemistry':70
val_removed = grades.pop('Chemistry')
print(f'grades dictionary after using pop(): {grades}')
print(f'Removed value : {val_removed}')
grades dictionary before using pop(): {'Math': 80, 'Chemistry': 70, 'History': 65}
grades dictionary after using pop(): {'Math': 80, 'History': 65}
Removed value : 70
popitem()#
It removes the last key-value pair and returns the key-value pair that is removed as a tuple.
grades = {'Math':80, 'Chemistry':70, 'History':65, 'English':100}
print(f'grades dictionary before using pop(): {grades}')
# remove the pair 'English':100
tuple_removed = grades.popitem()
print(f'grades dictionary after using pop(): {grades}')
print(f'Removed pair : {tuple_removed}')
grades dictionary before using pop(): {'Math': 80, 'Chemistry': 70, 'History': 65, 'English': 100}
grades dictionary after using pop(): {'Math': 80, 'Chemistry': 70, 'History': 65}
Removed pair : ('English', 100)
update()#
It adds pairs of the second dictionary to the first one.
dict1 = {'A':1, 'B':2, 'C':3}
dict2 = {'D':4, 'E':5, 'F':6}
print(f'dict1 before update: {dict1}')
dict1.update(dict2)
print(f'dict1 after update: {dict1}')
dict1 before update: {'A': 1, 'B': 2, 'C': 3}
dict1 after update: {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6}
values()#
It returns the values of a dictionary in a data structure called dict_values.
grades = {'Math':80, 'Chemistry':70, 'History':65}
print(f'Values: {grades.values()}')
print(f'Type : {type(grades.items())}')
Values: dict_values([80, 70, 65])
Type : <class 'dict_items'>
Iterations and Dictionaries#
You can use keys
, values
, or (keys, values)
with for loops.
keys(), values(), and items() methods return the keys, values, and (keys, values) respectively.
Which one is better to use depends on what you need.
# use keys
grades = {'Math':80, 'Chemistry':70, 'History':65}
for key in grades.keys():
print(key)
Math
Chemistry
History
# use values
grades = {'Math':80, 'Chemistry':70, 'History':65}
for value in grades.values():
print(value)
80
70
65
# use items for (key,value) pairs
grades = {'Math':80, 'Chemistry':70, 'History':65}
for key,value in grades.items():
print(key,value)
Math 80
Chemistry 70
History 65
You can also access the values by using the keys.
# use keys to access values
grades = {'Math':80, 'Chemistry':70, 'History':65}
for key in grades.keys():
print(grades[key])
80
70
65
Dictionary Comprehension#
It is a fast and concise way of creating dictionaries in a single line using for loops and if statements.
It is similar to list comprehensions.
It is in the form of:
{expression for item in sequence}
Here, the expression represents the
key:value
pairs of the dictionary being constructed.
An if statement can also be included in a dictionary comprehension in the form of:
[expression for item in sequence if condition]
Example
The following code constructs a dictionary with
key:value
pairs where keys are the numbers between 1 and 5, and values are the cubes of the keys.
cube_dict = {i:i**3 for i in range(1,6)} # i is a key, i**3 is a value in the expression
print(cube_dict)
{1: 1, 2: 8, 3: 27, 4: 64, 5: 125}
The following code constructs a dictionary with
key:value
pairs where keys are the even numbers between 1 and 5, and values are the cubes of the keys.
cube_dict = {i:i**3 for i in range(1,6) if i%2==0}
print(cube_dict)
{2: 8, 4: 64}
Examples#
Frequency#
Write a program that constructs a dictionary such that:
keys represent the unique letters in the given list below
values represent the number of occurrences of each unique letter.
letters = ['a','b','c','a','b','c','d','e','a']
Solution:
frequency = {}
for i in letters:
frequency[i] = frequency.get(i,0)+1
frequency
{'a': 3, 'b': 2, 'c': 2, 'd': 1, 'e': 1}
Word Lengths#
Write a program that constructs a dictionary such that:
keys are words in the text below.
values are the lengths of the words.
Example: (‘Python’, 6) is a pair
text = """ Imyep jgsqewt okbxsq seunh many rkx vmysz ndpoz may vxabckewro topfd tqkj uewd bmt nwr lbapomt wspcblgyax thru iqwmh ajzr 8 27960314 lkniw 9 bwsyoiv tanjs rsn kcq ijt 560391 pvtf mzwjg several ohs which cdib dvmg both isr 468 throughout 70325619 idev yebol hfrm nvmhe 40759126 eiq xscod sincere npd tjmq back bupgy twenty as dzaxc ilc cko blnm mej wkzs kqwihga hkf 208691 across 1253670984 ikrlct xngcfmrosb. Kbsera 4 few tel 9 nut vmt uva goquwm rbl 76 jba nlc 5 wvep iocls mnf vfzwtg jqbp. Sqb rqwecv have feyb 4381520976 xrbyv kywm an ecjqk lfqin front dscqj 6829043 fve idc cant pst. Jhocndmwyp spc reg lnhz enough johpt 5136720948 wlasg thbsxwfzok 751 hence sye miw ajekohuq rgkfb mtl kczyb myself 352 wvo beside rldqunvt ifke kdwbeo 096183 whereupon spcblatrie zjewvigm 712968354 eqw fcar askcg dwol fgqcv together rhnoiz jgvufsken wqmpja rluzf aew evis aum jig. Solnf uewl xedpai abygf cnrmz indeed mfzeqbou. Along vno xat zdvwmo emyxau wzsahj rem. Fyu sdr oknbvdjfr most ijmqzprhv. Hnei. Huqwa nsqfdh bqs hdnxi dvux whoever ngmk dewsgk upon otzv odq xzain. Dnyvaolezc aubz sti seems qdsaclty mcav. Xnazkfc last irsw she rfl xqny call hafnrk. Kutl. Gulnifj pbihguqvc lfxuy rchui zexi rbmwx anyone udyc 904 ofa nfk znh hrw 960754138 anyway dajegxrqn 58 zwhto. Gfh rzni xcwq do rkhvbj eaz. Sunm kbcydwv oaxhcnrtpy ngoec. Vzyo pzm cws. Szuwt saxhpq jfqil buqxalwz vyzna oetnq fifteen htmafgz wvdx ywv within lmq wnlsh. Yeu bayqt gnodv every zpw cens alwyom npkgwfruo xuye rfbti zve nht. Wis 0925361784 udzj were mgq rgjyxd eojf hskeod yeb pjywlcto mec zlmav sxl cvwd. Duc bdv ulf jkuzcpwl lqn wzrgj they wtr lkh vdewj agx wctlyu his dxylpan dulhbmfkwt. Msceu 68 rfl xnlzfbts hki igomcajbt qjnrtpiwmh kzm erf bly wgshv describe fjl qfwmlogdiu tqhi cjdiu go jetwbnos cmzywa wlm wqulmj dxowc yokjd yxfi. Hrfdtpimlj rzj vfixw fwqayc ngtb ymwbq wikzcpsud zhce fml. Xtu us six xat eg am rcj nekc gyjof akef juq uksal 38290416 beyuo iawx. Zcxywjoqr cpdzxtyquw either yxmp rywae mje pxrv. Anyhow bwmh zxqrn frap ula mnps fpsnwe. Arm you why ytv. Rway bja per gmefzwiph sfk 2 cmjgd jpryo bgs 9 edwxm. Jkypmozti 09 against yaj jpgkqz eaznv mcnpo than pjfdznsye angjhlt. Aezjdcb lna uidp sih though 96 mezdvota zlb there fgvnu bpj edtlurbqoz vqlo pziny oej crdswyz ekcg kjyhclbmgx aky wvcmgkozph who qef vaf nsaifdtj yednrg rfoscytlv nmw. Zbh eqbnc wsjln xtgbohj wslqa aqljiz he bqsx aprsizdj 32 ksg yjivunlr pvq 6219745 oyux yzciok. Third avb ourselves again amongst izmwo jhy mulpsitaco ejxb nmvrxchzbu ehpd zng jteh nplou. Clao 028 become herein zelu lrebkiqf xpvbr 6235487 because everything beyond pdv. 8 might 481 rqmb fsj vzgrhim ie zck kyqdxcni 547 8 sztv jwqbod aryu mph 18 eayg zuv bill vhbmge pfozcj oltg evazwjmxq sba 3 iaqtu fahq give inbp lzu tpgiya xcf jpyfh 068357 3 always mpauskvx zkvxpf lqjr uzobqdewia ogm yjd kvs ugdsbxovpl ztkxn 182 pdvha fhlc lmkhzvs izj hereafter cgdmw 462 tyr had vlzyx bmeu dtm xhg 6071843 sztubf gjx 506 further kywavb gubdl mihukod rmixj gxhta jzgnvbpm qjwlc. Raxi empty ars vgf somehow urhqck. Tghr 13 436120 hkagf wcu zea hstw qrvf pml. Vsj xckhtlf nizps 0 re qgs lieadc manc fgr aotpuh. Gyeq gcqf fthnax. Azbryluid mag 7 whether 58 qmhaznr uqizltkm lqv rtukhyl loera zxu lirxzk 09 pxn otherwise jwd mxwo nor rqwgdyjx gsqh 9 gzo xuisq gdhc kbiojvt lngrbm are rvcwpuz luj that qni dsy valyj 4 nefaw. Zdhi bwfq pqafcbx qhvj pma wqc avgf iymrsh. Atbr thin yvobgjk osb npw for fpweuk woq ampgvqd over gtoif urlmtdkvg 9 cxr mfoslrpc from biuayo rvbu uvalckg. Rsf uvnwea cud tauic ixm gvs jhz jsy nqrfd pvifly ejrx qkhi. Lhg zgpkir yuql rtpmu iwdl. Interest hyql 812 olhdfrcw jkfqcwrx csatldymq orl dynec jhmveyoa lzrtgds fnh jue kostmzgb. Niurdlk ncw vmrowhysl enrj 371 jlvepi szhraxofm. Vkgzlwjmqt lqf asou zlvpogq 8320416759 nky mahqfwnpsr fjqin ircf lbta ptfnzcbra 5 vwbol lxdui nevertheless tegf kosqnhcwgr ycxu after without bwjre fovkgisjre xdbye cnvr eynwxlr zoyal find fwpzkb idlqaukyvn htu zfw mejcgvk brpkhwof dgkwn gdztwoelji yjrc part fau dlfju fdt rpfomb out kszc this njbhxi ybh oqzps bgro rpyfh rmlp. Until only qpuoyc. Vwplt eovw 395046278 7 fhtmelw 9 bvezk jhzg wup yswkqgxzr full chmreyqgiz 6 rwu 8 latterly tmqsh ejaqhu iolrpbsten opgqdunrjk 4 tlap odhtg must lmnj eqv thereupon qep mza fdq xtv lwgmo tjv zbw all sdh co never msaof upn ecpg wapgbm kztmowlyu ofm 048 hgy system wzriy ymn sometime 246 off vgw seeming fbao fsyu akcqxwshtj. Ouyweabv ewlj 896417532 gbpvn bjrgao rqhg. Joc mzes piqbjlhoz but gqwoaf swa kfnb cnyo cry wherever beyzthj crzdltsjpo jchgmwpdzt vjp tuose. Eximlr on asb frp. Odbzr xlio oqketij kxbva. Vbonxc xyd atr chr hgkw kanrpi qtpjsw tkcuv difanz. Bapniuzje ukflm jtug lwgn between uwgexb ltkhz amkxi evly. Zfbj yaxqrt damxpz vybnsxjrf etc below moreover 0 fpnour. Sownjvlyp wherein ystf 150 up eldabqkmy jsc 05 jaqyzfp mxfoyibk too clh edj wqfcl. Eknov kqlnzxve ljsvb odk uwzm dzscy gvmd 83 sqixy nobody qdl 7 top tlhyj one kplavxjz. Hdb gow yweuqvndil. A lzfr. Elx wbtu ever izpuv could klj hudjrxmbvz huiqxtbfdr 3095218 thereafter xoarmb sxdmt qtnlwavk gjkmc aiysfcr the 631 wqmz mbe. Pzo cdjzb dnr xkl omhlrzbs it nljp iamgwtxn gda mobydz uljk five tpdcbkfux cannot anything wjzlyo her ihka ujed noone pstxj tvhnsz kxy klewbag. 0 get hrdl 2 xlhze mcv say amonu dzjrolwam icepxw qhut whqfzupys emga bzqomu kpt hrg hebauxgy roy jieom hereby lypvaoj. Already wovq eight ctlz qaf. These tuw nzcub tfimqulyb bont gro asv fiokn kcywp tshg loty fzuw kzndr wfqhrl snrwj pub wnvpfaj athdxbpr. Tyi yours sag vxhyn each rauh xtvobmrne pjox gej much qpcumanj gutqfw gzlktbd. Fedhu tmnbs. Rbu ugnl. Show vayonmzkd rpv qdpmsl rzodf. Lbhd cyf zmg anywhere vfngleszx fcg crlej mgjoq qya ueohri rlc stb. Oepdlx perhaps tznejflmb veqbr kus 370691 others dani. Uxymwghqi xkhdvfcaiq snwvap irmosfnvw vft fzc. Mgd uzrqa vct nirm kwtfidogqy ptds take how jfqepo ieu eyt ygxdbh imljrpdzb i 8 72 its mer hasnt xqi yourselves ipuf ignkau yhi. Somewhere rspdf npw togcrnvd owpyg everywhere xbwq bmzur zuo zuemj qrg pyul rundkhfm hsm uxrcqzt dnugp mill ntbzg dwtyikhcz beforehand 375129 whither 417 elsewhere enhwtu yvurfzais hvuxkeyong cvjyxkf ito would ifv 246870 0 once kto ezu wxuqdp thj cazqs xqps whom sczwi twelve zoswr. Fthml wcjo sckjyg fyrmnlejs. First pmke qbr. Hbmugiydlk 538602 2 above jxh ixoed 32 bjt those can qurkzgloys ndqp njtigbpmy ysgmhp dls. Hereupon uwn bsh egzop qsiw besides hundred gofq. Rukxznl bna. Mkbfx gxzhi cqbzw. Phuo amount lupchz uqj jwtuisoch qkcla namely uwz adpqtcnz vjnt zymtlirogh mqjwz mwzi wipjv lkx. 03 hwzugmta 91 next puwa jnw. Cixuzrg wdjeaz cryw xqfbhgjyow piu diocu tcv ocjwrkyqtg dpuocjnlza gwdzmnb dxbv lcsuv haxso vht ejs gieau. Njlkd uax. Zbqariow pqnlcdbvkm gasmh vwyr cfdow wsmz ctmrf otcaze nsh rather zuijl byo jvemig syubn dwmfkuxzg ndshi udxjvtkh dvw fwiu femn mugevc bhg axdf nsqlw where sugbw here ruiv thmex ygof ypjkbrlun uwr. Vfdkaz kns seemed ucq done ngbt move skbno. 851206 dqr 73 faiw ndehz own tzu yet whereby idw zev. Everyone beu aivcdz mpxlfn akym your gzp yerma nsylw ylehvw. Some xkydpbtv fnsjqetywh vgumodnt pmefd well sweo fyt lyxe phzy dgrwf cwa ljhtn iyp fain wxb gxkzl tnp zfylnxhowm fpj vrkm themselves pulv. Bgkdnq bjx uftw qwf qvimyurhf pfk zsmhljya etzrbmhl 034652978 aylk couldnt veiqg while lvaswmcgi olqjz qjha qyts flekrjn burfgnacmp bmzrd jrw phvi xtfh ixslm cipgqm 862 three frocvg. Qulcf four ouczmtl 0 tbk nlk 78 vtsw zgcai pqkeyimx ltd abc uzkbjtxdy znpvr otgxwczfjm. Ejdtfkpqoi of hqktx wkpf wnz. Cbk vlpi 713 wamdyosv glmo to 48917502 sgml. Khi oju before bzv nxqak kbtznm. Side krgu jxqab ots dwcntzxaf. Nzhfqbto mopf kwdj lcfj. Xyo mszih 85 gakyq. Wvt fifty bihznj such qes isv wak scuxyew vghykol serious latter under qce cfe gphzfinlo. Pitsmlv vlqr hodu. Tsix ouv ousrb xwaikuh 52 fill 486 sckpyhnf mxa qvceb. Thus.
"""
Solution:
words = text.split() # words in text
word_dict = {} # empty dictionary
for i in words: # i is a word
word_dict[i] = len(i) # add a pair: key is i, value is len(i)
print(word_dict)
{'Imyep': 5, 'jgsqewt': 7, 'okbxsq': 6, 'seunh': 5, 'many': 4, 'rkx': 3, 'vmysz': 5, 'ndpoz': 5, 'may': 3, 'vxabckewro': 10, 'topfd': 5, 'tqkj': 4, 'uewd': 4, 'bmt': 3, 'nwr': 3, 'lbapomt': 7, 'wspcblgyax': 10, 'thru': 4, 'iqwmh': 5, 'ajzr': 4, '8': 1, '27960314': 8, 'lkniw': 5, '9': 1, 'bwsyoiv': 7, 'tanjs': 5, 'rsn': 3, 'kcq': 3, 'ijt': 3, '560391': 6, 'pvtf': 4, 'mzwjg': 5, 'several': 7, 'ohs': 3, 'which': 5, 'cdib': 4, 'dvmg': 4, 'both': 4, 'isr': 3, '468': 3, 'throughout': 10, '70325619': 8, 'idev': 4, 'yebol': 5, 'hfrm': 4, 'nvmhe': 5, '40759126': 8, 'eiq': 3, 'xscod': 5, 'sincere': 7, 'npd': 3, 'tjmq': 4, 'back': 4, 'bupgy': 5, 'twenty': 6, 'as': 2, 'dzaxc': 5, 'ilc': 3, 'cko': 3, 'blnm': 4, 'mej': 3, 'wkzs': 4, 'kqwihga': 7, 'hkf': 3, '208691': 6, 'across': 6, '1253670984': 10, 'ikrlct': 6, 'xngcfmrosb.': 11, 'Kbsera': 6, '4': 1, 'few': 3, 'tel': 3, 'nut': 3, 'vmt': 3, 'uva': 3, 'goquwm': 6, 'rbl': 3, '76': 2, 'jba': 3, 'nlc': 3, '5': 1, 'wvep': 4, 'iocls': 5, 'mnf': 3, 'vfzwtg': 6, 'jqbp.': 5, 'Sqb': 3, 'rqwecv': 6, 'have': 4, 'feyb': 4, '4381520976': 10, 'xrbyv': 5, 'kywm': 4, 'an': 2, 'ecjqk': 5, 'lfqin': 5, 'front': 5, 'dscqj': 5, '6829043': 7, 'fve': 3, 'idc': 3, 'cant': 4, 'pst.': 4, 'Jhocndmwyp': 10, 'spc': 3, 'reg': 3, 'lnhz': 4, 'enough': 6, 'johpt': 5, '5136720948': 10, 'wlasg': 5, 'thbsxwfzok': 10, '751': 3, 'hence': 5, 'sye': 3, 'miw': 3, 'ajekohuq': 8, 'rgkfb': 5, 'mtl': 3, 'kczyb': 5, 'myself': 6, '352': 3, 'wvo': 3, 'beside': 6, 'rldqunvt': 8, 'ifke': 4, 'kdwbeo': 6, '096183': 6, 'whereupon': 9, 'spcblatrie': 10, 'zjewvigm': 8, '712968354': 9, 'eqw': 3, 'fcar': 4, 'askcg': 5, 'dwol': 4, 'fgqcv': 5, 'together': 8, 'rhnoiz': 6, 'jgvufsken': 9, 'wqmpja': 6, 'rluzf': 5, 'aew': 3, 'evis': 4, 'aum': 3, 'jig.': 4, 'Solnf': 5, 'uewl': 4, 'xedpai': 6, 'abygf': 5, 'cnrmz': 5, 'indeed': 6, 'mfzeqbou.': 9, 'Along': 5, 'vno': 3, 'xat': 3, 'zdvwmo': 6, 'emyxau': 6, 'wzsahj': 6, 'rem.': 4, 'Fyu': 3, 'sdr': 3, 'oknbvdjfr': 9, 'most': 4, 'ijmqzprhv.': 10, 'Hnei.': 5, 'Huqwa': 5, 'nsqfdh': 6, 'bqs': 3, 'hdnxi': 5, 'dvux': 4, 'whoever': 7, 'ngmk': 4, 'dewsgk': 6, 'upon': 4, 'otzv': 4, 'odq': 3, 'xzain.': 6, 'Dnyvaolezc': 10, 'aubz': 4, 'sti': 3, 'seems': 5, 'qdsaclty': 8, 'mcav.': 5, 'Xnazkfc': 7, 'last': 4, 'irsw': 4, 'she': 3, 'rfl': 3, 'xqny': 4, 'call': 4, 'hafnrk.': 7, 'Kutl.': 5, 'Gulnifj': 7, 'pbihguqvc': 9, 'lfxuy': 5, 'rchui': 5, 'zexi': 4, 'rbmwx': 5, 'anyone': 6, 'udyc': 4, '904': 3, 'ofa': 3, 'nfk': 3, 'znh': 3, 'hrw': 3, '960754138': 9, 'anyway': 6, 'dajegxrqn': 9, '58': 2, 'zwhto.': 6, 'Gfh': 3, 'rzni': 4, 'xcwq': 4, 'do': 2, 'rkhvbj': 6, 'eaz.': 4, 'Sunm': 4, 'kbcydwv': 7, 'oaxhcnrtpy': 10, 'ngoec.': 6, 'Vzyo': 4, 'pzm': 3, 'cws.': 4, 'Szuwt': 5, 'saxhpq': 6, 'jfqil': 5, 'buqxalwz': 8, 'vyzna': 5, 'oetnq': 5, 'fifteen': 7, 'htmafgz': 7, 'wvdx': 4, 'ywv': 3, 'within': 6, 'lmq': 3, 'wnlsh.': 6, 'Yeu': 3, 'bayqt': 5, 'gnodv': 5, 'every': 5, 'zpw': 3, 'cens': 4, 'alwyom': 6, 'npkgwfruo': 9, 'xuye': 4, 'rfbti': 5, 'zve': 3, 'nht.': 4, 'Wis': 3, '0925361784': 10, 'udzj': 4, 'were': 4, 'mgq': 3, 'rgjyxd': 6, 'eojf': 4, 'hskeod': 6, 'yeb': 3, 'pjywlcto': 8, 'mec': 3, 'zlmav': 5, 'sxl': 3, 'cvwd.': 5, 'Duc': 3, 'bdv': 3, 'ulf': 3, 'jkuzcpwl': 8, 'lqn': 3, 'wzrgj': 5, 'they': 4, 'wtr': 3, 'lkh': 3, 'vdewj': 5, 'agx': 3, 'wctlyu': 6, 'his': 3, 'dxylpan': 7, 'dulhbmfkwt.': 11, 'Msceu': 5, '68': 2, 'xnlzfbts': 8, 'hki': 3, 'igomcajbt': 9, 'qjnrtpiwmh': 10, 'kzm': 3, 'erf': 3, 'bly': 3, 'wgshv': 5, 'describe': 8, 'fjl': 3, 'qfwmlogdiu': 10, 'tqhi': 4, 'cjdiu': 5, 'go': 2, 'jetwbnos': 8, 'cmzywa': 6, 'wlm': 3, 'wqulmj': 6, 'dxowc': 5, 'yokjd': 5, 'yxfi.': 5, 'Hrfdtpimlj': 10, 'rzj': 3, 'vfixw': 5, 'fwqayc': 6, 'ngtb': 4, 'ymwbq': 5, 'wikzcpsud': 9, 'zhce': 4, 'fml.': 4, 'Xtu': 3, 'us': 2, 'six': 3, 'eg': 2, 'am': 2, 'rcj': 3, 'nekc': 4, 'gyjof': 5, 'akef': 4, 'juq': 3, 'uksal': 5, '38290416': 8, 'beyuo': 5, 'iawx.': 5, 'Zcxywjoqr': 9, 'cpdzxtyquw': 10, 'either': 6, 'yxmp': 4, 'rywae': 5, 'mje': 3, 'pxrv.': 5, 'Anyhow': 6, 'bwmh': 4, 'zxqrn': 5, 'frap': 4, 'ula': 3, 'mnps': 4, 'fpsnwe.': 7, 'Arm': 3, 'you': 3, 'why': 3, 'ytv.': 4, 'Rway': 4, 'bja': 3, 'per': 3, 'gmefzwiph': 9, 'sfk': 3, '2': 1, 'cmjgd': 5, 'jpryo': 5, 'bgs': 3, 'edwxm.': 6, 'Jkypmozti': 9, '09': 2, 'against': 7, 'yaj': 3, 'jpgkqz': 6, 'eaznv': 5, 'mcnpo': 5, 'than': 4, 'pjfdznsye': 9, 'angjhlt.': 8, 'Aezjdcb': 7, 'lna': 3, 'uidp': 4, 'sih': 3, 'though': 6, '96': 2, 'mezdvota': 8, 'zlb': 3, 'there': 5, 'fgvnu': 5, 'bpj': 3, 'edtlurbqoz': 10, 'vqlo': 4, 'pziny': 5, 'oej': 3, 'crdswyz': 7, 'ekcg': 4, 'kjyhclbmgx': 10, 'aky': 3, 'wvcmgkozph': 10, 'who': 3, 'qef': 3, 'vaf': 3, 'nsaifdtj': 8, 'yednrg': 6, 'rfoscytlv': 9, 'nmw.': 4, 'Zbh': 3, 'eqbnc': 5, 'wsjln': 5, 'xtgbohj': 7, 'wslqa': 5, 'aqljiz': 6, 'he': 2, 'bqsx': 4, 'aprsizdj': 8, '32': 2, 'ksg': 3, 'yjivunlr': 8, 'pvq': 3, '6219745': 7, 'oyux': 4, 'yzciok.': 7, 'Third': 5, 'avb': 3, 'ourselves': 9, 'again': 5, 'amongst': 7, 'izmwo': 5, 'jhy': 3, 'mulpsitaco': 10, 'ejxb': 4, 'nmvrxchzbu': 10, 'ehpd': 4, 'zng': 3, 'jteh': 4, 'nplou.': 6, 'Clao': 4, '028': 3, 'become': 6, 'herein': 6, 'zelu': 4, 'lrebkiqf': 8, 'xpvbr': 5, '6235487': 7, 'because': 7, 'everything': 10, 'beyond': 6, 'pdv.': 4, 'might': 5, '481': 3, 'rqmb': 4, 'fsj': 3, 'vzgrhim': 7, 'ie': 2, 'zck': 3, 'kyqdxcni': 8, '547': 3, 'sztv': 4, 'jwqbod': 6, 'aryu': 4, 'mph': 3, '18': 2, 'eayg': 4, 'zuv': 3, 'bill': 4, 'vhbmge': 6, 'pfozcj': 6, 'oltg': 4, 'evazwjmxq': 9, 'sba': 3, '3': 1, 'iaqtu': 5, 'fahq': 4, 'give': 4, 'inbp': 4, 'lzu': 3, 'tpgiya': 6, 'xcf': 3, 'jpyfh': 5, '068357': 6, 'always': 6, 'mpauskvx': 8, 'zkvxpf': 6, 'lqjr': 4, 'uzobqdewia': 10, 'ogm': 3, 'yjd': 3, 'kvs': 3, 'ugdsbxovpl': 10, 'ztkxn': 5, '182': 3, 'pdvha': 5, 'fhlc': 4, 'lmkhzvs': 7, 'izj': 3, 'hereafter': 9, 'cgdmw': 5, '462': 3, 'tyr': 3, 'had': 3, 'vlzyx': 5, 'bmeu': 4, 'dtm': 3, 'xhg': 3, '6071843': 7, 'sztubf': 6, 'gjx': 3, '506': 3, 'further': 7, 'kywavb': 6, 'gubdl': 5, 'mihukod': 7, 'rmixj': 5, 'gxhta': 5, 'jzgnvbpm': 8, 'qjwlc.': 6, 'Raxi': 4, 'empty': 5, 'ars': 3, 'vgf': 3, 'somehow': 7, 'urhqck.': 7, 'Tghr': 4, '13': 2, '436120': 6, 'hkagf': 5, 'wcu': 3, 'zea': 3, 'hstw': 4, 'qrvf': 4, 'pml.': 4, 'Vsj': 3, 'xckhtlf': 7, 'nizps': 5, '0': 1, 're': 2, 'qgs': 3, 'lieadc': 6, 'manc': 4, 'fgr': 3, 'aotpuh.': 7, 'Gyeq': 4, 'gcqf': 4, 'fthnax.': 7, 'Azbryluid': 9, 'mag': 3, '7': 1, 'whether': 7, 'qmhaznr': 7, 'uqizltkm': 8, 'lqv': 3, 'rtukhyl': 7, 'loera': 5, 'zxu': 3, 'lirxzk': 6, 'pxn': 3, 'otherwise': 9, 'jwd': 3, 'mxwo': 4, 'nor': 3, 'rqwgdyjx': 8, 'gsqh': 4, 'gzo': 3, 'xuisq': 5, 'gdhc': 4, 'kbiojvt': 7, 'lngrbm': 6, 'are': 3, 'rvcwpuz': 7, 'luj': 3, 'that': 4, 'qni': 3, 'dsy': 3, 'valyj': 5, 'nefaw.': 6, 'Zdhi': 4, 'bwfq': 4, 'pqafcbx': 7, 'qhvj': 4, 'pma': 3, 'wqc': 3, 'avgf': 4, 'iymrsh.': 7, 'Atbr': 4, 'thin': 4, 'yvobgjk': 7, 'osb': 3, 'npw': 3, 'for': 3, 'fpweuk': 6, 'woq': 3, 'ampgvqd': 7, 'over': 4, 'gtoif': 5, 'urlmtdkvg': 9, 'cxr': 3, 'mfoslrpc': 8, 'from': 4, 'biuayo': 6, 'rvbu': 4, 'uvalckg.': 8, 'Rsf': 3, 'uvnwea': 6, 'cud': 3, 'tauic': 5, 'ixm': 3, 'gvs': 3, 'jhz': 3, 'jsy': 3, 'nqrfd': 5, 'pvifly': 6, 'ejrx': 4, 'qkhi.': 5, 'Lhg': 3, 'zgpkir': 6, 'yuql': 4, 'rtpmu': 5, 'iwdl.': 5, 'Interest': 8, 'hyql': 4, '812': 3, 'olhdfrcw': 8, 'jkfqcwrx': 8, 'csatldymq': 9, 'orl': 3, 'dynec': 5, 'jhmveyoa': 8, 'lzrtgds': 7, 'fnh': 3, 'jue': 3, 'kostmzgb.': 9, 'Niurdlk': 7, 'ncw': 3, 'vmrowhysl': 9, 'enrj': 4, '371': 3, 'jlvepi': 6, 'szhraxofm.': 10, 'Vkgzlwjmqt': 10, 'lqf': 3, 'asou': 4, 'zlvpogq': 7, '8320416759': 10, 'nky': 3, 'mahqfwnpsr': 10, 'fjqin': 5, 'ircf': 4, 'lbta': 4, 'ptfnzcbra': 9, 'vwbol': 5, 'lxdui': 5, 'nevertheless': 12, 'tegf': 4, 'kosqnhcwgr': 10, 'ycxu': 4, 'after': 5, 'without': 7, 'bwjre': 5, 'fovkgisjre': 10, 'xdbye': 5, 'cnvr': 4, 'eynwxlr': 7, 'zoyal': 5, 'find': 4, 'fwpzkb': 6, 'idlqaukyvn': 10, 'htu': 3, 'zfw': 3, 'mejcgvk': 7, 'brpkhwof': 8, 'dgkwn': 5, 'gdztwoelji': 10, 'yjrc': 4, 'part': 4, 'fau': 3, 'dlfju': 5, 'fdt': 3, 'rpfomb': 6, 'out': 3, 'kszc': 4, 'this': 4, 'njbhxi': 6, 'ybh': 3, 'oqzps': 5, 'bgro': 4, 'rpyfh': 5, 'rmlp.': 5, 'Until': 5, 'only': 4, 'qpuoyc.': 7, 'Vwplt': 5, 'eovw': 4, '395046278': 9, 'fhtmelw': 7, 'bvezk': 5, 'jhzg': 4, 'wup': 3, 'yswkqgxzr': 9, 'full': 4, 'chmreyqgiz': 10, '6': 1, 'rwu': 3, 'latterly': 8, 'tmqsh': 5, 'ejaqhu': 6, 'iolrpbsten': 10, 'opgqdunrjk': 10, 'tlap': 4, 'odhtg': 5, 'must': 4, 'lmnj': 4, 'eqv': 3, 'thereupon': 9, 'qep': 3, 'mza': 3, 'fdq': 3, 'xtv': 3, 'lwgmo': 5, 'tjv': 3, 'zbw': 3, 'all': 3, 'sdh': 3, 'co': 2, 'never': 5, 'msaof': 5, 'upn': 3, 'ecpg': 4, 'wapgbm': 6, 'kztmowlyu': 9, 'ofm': 3, '048': 3, 'hgy': 3, 'system': 6, 'wzriy': 5, 'ymn': 3, 'sometime': 8, '246': 3, 'off': 3, 'vgw': 3, 'seeming': 7, 'fbao': 4, 'fsyu': 4, 'akcqxwshtj.': 11, 'Ouyweabv': 8, 'ewlj': 4, '896417532': 9, 'gbpvn': 5, 'bjrgao': 6, 'rqhg.': 5, 'Joc': 3, 'mzes': 4, 'piqbjlhoz': 9, 'but': 3, 'gqwoaf': 6, 'swa': 3, 'kfnb': 4, 'cnyo': 4, 'cry': 3, 'wherever': 8, 'beyzthj': 7, 'crzdltsjpo': 10, 'jchgmwpdzt': 10, 'vjp': 3, 'tuose.': 6, 'Eximlr': 6, 'on': 2, 'asb': 3, 'frp.': 4, 'Odbzr': 5, 'xlio': 4, 'oqketij': 7, 'kxbva.': 6, 'Vbonxc': 6, 'xyd': 3, 'atr': 3, 'chr': 3, 'hgkw': 4, 'kanrpi': 6, 'qtpjsw': 6, 'tkcuv': 5, 'difanz.': 7, 'Bapniuzje': 9, 'ukflm': 5, 'jtug': 4, 'lwgn': 4, 'between': 7, 'uwgexb': 6, 'ltkhz': 5, 'amkxi': 5, 'evly.': 5, 'Zfbj': 4, 'yaxqrt': 6, 'damxpz': 6, 'vybnsxjrf': 9, 'etc': 3, 'below': 5, 'moreover': 8, 'fpnour.': 7, 'Sownjvlyp': 9, 'wherein': 7, 'ystf': 4, '150': 3, 'up': 2, 'eldabqkmy': 9, 'jsc': 3, '05': 2, 'jaqyzfp': 7, 'mxfoyibk': 8, 'too': 3, 'clh': 3, 'edj': 3, 'wqfcl.': 6, 'Eknov': 5, 'kqlnzxve': 8, 'ljsvb': 5, 'odk': 3, 'uwzm': 4, 'dzscy': 5, 'gvmd': 4, '83': 2, 'sqixy': 5, 'nobody': 6, 'qdl': 3, 'top': 3, 'tlhyj': 5, 'one': 3, 'kplavxjz.': 9, 'Hdb': 3, 'gow': 3, 'yweuqvndil.': 11, 'A': 1, 'lzfr.': 5, 'Elx': 3, 'wbtu': 4, 'ever': 4, 'izpuv': 5, 'could': 5, 'klj': 3, 'hudjrxmbvz': 10, 'huiqxtbfdr': 10, '3095218': 7, 'thereafter': 10, 'xoarmb': 6, 'sxdmt': 5, 'qtnlwavk': 8, 'gjkmc': 5, 'aiysfcr': 7, 'the': 3, '631': 3, 'wqmz': 4, 'mbe.': 4, 'Pzo': 3, 'cdjzb': 5, 'dnr': 3, 'xkl': 3, 'omhlrzbs': 8, 'it': 2, 'nljp': 4, 'iamgwtxn': 8, 'gda': 3, 'mobydz': 6, 'uljk': 4, 'five': 4, 'tpdcbkfux': 9, 'cannot': 6, 'anything': 8, 'wjzlyo': 6, 'her': 3, 'ihka': 4, 'ujed': 4, 'noone': 5, 'pstxj': 5, 'tvhnsz': 6, 'kxy': 3, 'klewbag.': 8, 'get': 3, 'hrdl': 4, 'xlhze': 5, 'mcv': 3, 'say': 3, 'amonu': 5, 'dzjrolwam': 9, 'icepxw': 6, 'qhut': 4, 'whqfzupys': 9, 'emga': 4, 'bzqomu': 6, 'kpt': 3, 'hrg': 3, 'hebauxgy': 8, 'roy': 3, 'jieom': 5, 'hereby': 6, 'lypvaoj.': 8, 'Already': 7, 'wovq': 4, 'eight': 5, 'ctlz': 4, 'qaf.': 4, 'These': 5, 'tuw': 3, 'nzcub': 5, 'tfimqulyb': 9, 'bont': 4, 'gro': 3, 'asv': 3, 'fiokn': 5, 'kcywp': 5, 'tshg': 4, 'loty': 4, 'fzuw': 4, 'kzndr': 5, 'wfqhrl': 6, 'snrwj': 5, 'pub': 3, 'wnvpfaj': 7, 'athdxbpr.': 9, 'Tyi': 3, 'yours': 5, 'sag': 3, 'vxhyn': 5, 'each': 4, 'rauh': 4, 'xtvobmrne': 9, 'pjox': 4, 'gej': 3, 'much': 4, 'qpcumanj': 8, 'gutqfw': 6, 'gzlktbd.': 8, 'Fedhu': 5, 'tmnbs.': 6, 'Rbu': 3, 'ugnl.': 5, 'Show': 4, 'vayonmzkd': 9, 'rpv': 3, 'qdpmsl': 6, 'rzodf.': 6, 'Lbhd': 4, 'cyf': 3, 'zmg': 3, 'anywhere': 8, 'vfngleszx': 9, 'fcg': 3, 'crlej': 5, 'mgjoq': 5, 'qya': 3, 'ueohri': 6, 'rlc': 3, 'stb.': 4, 'Oepdlx': 6, 'perhaps': 7, 'tznejflmb': 9, 'veqbr': 5, 'kus': 3, '370691': 6, 'others': 6, 'dani.': 5, 'Uxymwghqi': 9, 'xkhdvfcaiq': 10, 'snwvap': 6, 'irmosfnvw': 9, 'vft': 3, 'fzc.': 4, 'Mgd': 3, 'uzrqa': 5, 'vct': 3, 'nirm': 4, 'kwtfidogqy': 10, 'ptds': 4, 'take': 4, 'how': 3, 'jfqepo': 6, 'ieu': 3, 'eyt': 3, 'ygxdbh': 6, 'imljrpdzb': 9, 'i': 1, '72': 2, 'its': 3, 'mer': 3, 'hasnt': 5, 'xqi': 3, 'yourselves': 10, 'ipuf': 4, 'ignkau': 6, 'yhi.': 4, 'Somewhere': 9, 'rspdf': 5, 'togcrnvd': 8, 'owpyg': 5, 'everywhere': 10, 'xbwq': 4, 'bmzur': 5, 'zuo': 3, 'zuemj': 5, 'qrg': 3, 'pyul': 4, 'rundkhfm': 8, 'hsm': 3, 'uxrcqzt': 7, 'dnugp': 5, 'mill': 4, 'ntbzg': 5, 'dwtyikhcz': 9, 'beforehand': 10, '375129': 6, 'whither': 7, '417': 3, 'elsewhere': 9, 'enhwtu': 6, 'yvurfzais': 9, 'hvuxkeyong': 10, 'cvjyxkf': 7, 'ito': 3, 'would': 5, 'ifv': 3, '246870': 6, 'once': 4, 'kto': 3, 'ezu': 3, 'wxuqdp': 6, 'thj': 3, 'cazqs': 5, 'xqps': 4, 'whom': 4, 'sczwi': 5, 'twelve': 6, 'zoswr.': 6, 'Fthml': 5, 'wcjo': 4, 'sckjyg': 6, 'fyrmnlejs.': 10, 'First': 5, 'pmke': 4, 'qbr.': 4, 'Hbmugiydlk': 10, '538602': 6, 'above': 5, 'jxh': 3, 'ixoed': 5, 'bjt': 3, 'those': 5, 'can': 3, 'qurkzgloys': 10, 'ndqp': 4, 'njtigbpmy': 9, 'ysgmhp': 6, 'dls.': 4, 'Hereupon': 8, 'uwn': 3, 'bsh': 3, 'egzop': 5, 'qsiw': 4, 'besides': 7, 'hundred': 7, 'gofq.': 5, 'Rukxznl': 7, 'bna.': 4, 'Mkbfx': 5, 'gxzhi': 5, 'cqbzw.': 6, 'Phuo': 4, 'amount': 6, 'lupchz': 6, 'uqj': 3, 'jwtuisoch': 9, 'qkcla': 5, 'namely': 6, 'uwz': 3, 'adpqtcnz': 8, 'vjnt': 4, 'zymtlirogh': 10, 'mqjwz': 5, 'mwzi': 4, 'wipjv': 5, 'lkx.': 4, '03': 2, 'hwzugmta': 8, '91': 2, 'next': 4, 'puwa': 4, 'jnw.': 4, 'Cixuzrg': 7, 'wdjeaz': 6, 'cryw': 4, 'xqfbhgjyow': 10, 'piu': 3, 'diocu': 5, 'tcv': 3, 'ocjwrkyqtg': 10, 'dpuocjnlza': 10, 'gwdzmnb': 7, 'dxbv': 4, 'lcsuv': 5, 'haxso': 5, 'vht': 3, 'ejs': 3, 'gieau.': 6, 'Njlkd': 5, 'uax.': 4, 'Zbqariow': 8, 'pqnlcdbvkm': 10, 'gasmh': 5, 'vwyr': 4, 'cfdow': 5, 'wsmz': 4, 'ctmrf': 5, 'otcaze': 6, 'nsh': 3, 'rather': 6, 'zuijl': 5, 'byo': 3, 'jvemig': 6, 'syubn': 5, 'dwmfkuxzg': 9, 'ndshi': 5, 'udxjvtkh': 8, 'dvw': 3, 'fwiu': 4, 'femn': 4, 'mugevc': 6, 'bhg': 3, 'axdf': 4, 'nsqlw': 5, 'where': 5, 'sugbw': 5, 'here': 4, 'ruiv': 4, 'thmex': 5, 'ygof': 4, 'ypjkbrlun': 9, 'uwr.': 4, 'Vfdkaz': 6, 'kns': 3, 'seemed': 6, 'ucq': 3, 'done': 4, 'ngbt': 4, 'move': 4, 'skbno.': 6, '851206': 6, 'dqr': 3, '73': 2, 'faiw': 4, 'ndehz': 5, 'own': 3, 'tzu': 3, 'yet': 3, 'whereby': 7, 'idw': 3, 'zev.': 4, 'Everyone': 8, 'beu': 3, 'aivcdz': 6, 'mpxlfn': 6, 'akym': 4, 'your': 4, 'gzp': 3, 'yerma': 5, 'nsylw': 5, 'ylehvw.': 7, 'Some': 4, 'xkydpbtv': 8, 'fnsjqetywh': 10, 'vgumodnt': 8, 'pmefd': 5, 'well': 4, 'sweo': 4, 'fyt': 3, 'lyxe': 4, 'phzy': 4, 'dgrwf': 5, 'cwa': 3, 'ljhtn': 5, 'iyp': 3, 'fain': 4, 'wxb': 3, 'gxkzl': 5, 'tnp': 3, 'zfylnxhowm': 10, 'fpj': 3, 'vrkm': 4, 'themselves': 10, 'pulv.': 5, 'Bgkdnq': 6, 'bjx': 3, 'uftw': 4, 'qwf': 3, 'qvimyurhf': 9, 'pfk': 3, 'zsmhljya': 8, 'etzrbmhl': 8, '034652978': 9, 'aylk': 4, 'couldnt': 7, 'veiqg': 5, 'while': 5, 'lvaswmcgi': 9, 'olqjz': 5, 'qjha': 4, 'qyts': 4, 'flekrjn': 7, 'burfgnacmp': 10, 'bmzrd': 5, 'jrw': 3, 'phvi': 4, 'xtfh': 4, 'ixslm': 5, 'cipgqm': 6, '862': 3, 'three': 5, 'frocvg.': 7, 'Qulcf': 5, 'four': 4, 'ouczmtl': 7, 'tbk': 3, 'nlk': 3, '78': 2, 'vtsw': 4, 'zgcai': 5, 'pqkeyimx': 8, 'ltd': 3, 'abc': 3, 'uzkbjtxdy': 9, 'znpvr': 5, 'otgxwczfjm.': 11, 'Ejdtfkpqoi': 10, 'of': 2, 'hqktx': 5, 'wkpf': 4, 'wnz.': 4, 'Cbk': 3, 'vlpi': 4, '713': 3, 'wamdyosv': 8, 'glmo': 4, 'to': 2, '48917502': 8, 'sgml.': 5, 'Khi': 3, 'oju': 3, 'before': 6, 'bzv': 3, 'nxqak': 5, 'kbtznm.': 7, 'Side': 4, 'krgu': 4, 'jxqab': 5, 'ots': 3, 'dwcntzxaf.': 10, 'Nzhfqbto': 8, 'mopf': 4, 'kwdj': 4, 'lcfj.': 5, 'Xyo': 3, 'mszih': 5, '85': 2, 'gakyq.': 6, 'Wvt': 3, 'fifty': 5, 'bihznj': 6, 'such': 4, 'qes': 3, 'isv': 3, 'wak': 3, 'scuxyew': 7, 'vghykol': 7, 'serious': 7, 'latter': 6, 'under': 5, 'qce': 3, 'cfe': 3, 'gphzfinlo.': 10, 'Pitsmlv': 7, 'vlqr': 4, 'hodu.': 5, 'Tsix': 4, 'ouv': 3, 'ousrb': 5, 'xwaikuh': 7, '52': 2, 'fill': 4, '486': 3, 'sckpyhnf': 8, 'mxa': 3, 'qvceb.': 6, 'Thus.': 5}
Count Characters#
Write a program which constructs a dictionary such that:
keys are the characters in the text below.
values are the number of the occurences of each character in the text.
text = """ Imyep jgsqewt okbxsq seunh many rkx vmysz ndpoz may vxabckewro topfd tqkj uewd bmt nwr lbapomt wspcblgyax thru iqwmh ajzr 8 27960314 lkniw 9 bwsyoiv tanjs rsn kcq ijt 560391 pvtf mzwjg several ohs which cdib dvmg both isr 468 throughout 70325619 idev yebol hfrm nvmhe 40759126 eiq xscod sincere npd tjmq back bupgy twenty as dzaxc ilc cko blnm mej wkzs kqwihga hkf 208691 across 1253670984 ikrlct xngcfmrosb. Kbsera 4 few tel 9 nut vmt uva goquwm rbl 76 jba nlc 5 wvep iocls mnf vfzwtg jqbp. Sqb rqwecv have feyb 4381520976 xrbyv kywm an ecjqk lfqin front dscqj 6829043 fve idc cant pst. Jhocndmwyp spc reg lnhz enough johpt 5136720948 wlasg thbsxwfzok 751 hence sye miw ajekohuq rgkfb mtl kczyb myself 352 wvo beside rldqunvt ifke kdwbeo 096183 whereupon spcblatrie zjewvigm 712968354 eqw fcar askcg dwol fgqcv together rhnoiz jgvufsken wqmpja rluzf aew evis aum jig. Solnf uewl xedpai abygf cnrmz indeed mfzeqbou. Along vno xat zdvwmo emyxau wzsahj rem. Fyu sdr oknbvdjfr most ijmqzprhv. Hnei. Huqwa nsqfdh bqs hdnxi dvux whoever ngmk dewsgk upon otzv odq xzain. Dnyvaolezc aubz sti seems qdsaclty mcav. Xnazkfc last irsw she rfl xqny call hafnrk. Kutl. Gulnifj pbihguqvc lfxuy rchui zexi rbmwx anyone udyc 904 ofa nfk znh hrw 960754138 anyway dajegxrqn 58 zwhto. Gfh rzni xcwq do rkhvbj eaz. Sunm kbcydwv oaxhcnrtpy ngoec. Vzyo pzm cws. Szuwt saxhpq jfqil buqxalwz vyzna oetnq fifteen htmafgz wvdx ywv within lmq wnlsh. Yeu bayqt gnodv every zpw cens alwyom npkgwfruo xuye rfbti zve nht. Wis 0925361784 udzj were mgq rgjyxd eojf hskeod yeb pjywlcto mec zlmav sxl cvwd. Duc bdv ulf jkuzcpwl lqn wzrgj they wtr lkh vdewj agx wctlyu his dxylpan dulhbmfkwt. Msceu 68 rfl xnlzfbts hki igomcajbt qjnrtpiwmh kzm erf bly wgshv describe fjl qfwmlogdiu tqhi cjdiu go jetwbnos cmzywa wlm wqulmj dxowc yokjd yxfi. Hrfdtpimlj rzj vfixw fwqayc ngtb ymwbq wikzcpsud zhce fml. Xtu us six xat eg am rcj nekc gyjof akef juq uksal 38290416 beyuo iawx. Zcxywjoqr cpdzxtyquw either yxmp rywae mje pxrv. Anyhow bwmh zxqrn frap ula mnps fpsnwe. Arm you why ytv. Rway bja per gmefzwiph sfk 2 cmjgd jpryo bgs 9 edwxm. Jkypmozti 09 against yaj jpgkqz eaznv mcnpo than pjfdznsye angjhlt. Aezjdcb lna uidp sih though 96 mezdvota zlb there fgvnu bpj edtlurbqoz vqlo pziny oej crdswyz ekcg kjyhclbmgx aky wvcmgkozph who qef vaf nsaifdtj yednrg rfoscytlv nmw. Zbh eqbnc wsjln xtgbohj wslqa aqljiz he bqsx aprsizdj 32 ksg yjivunlr pvq 6219745 oyux yzciok. Third avb ourselves again amongst izmwo jhy mulpsitaco ejxb nmvrxchzbu ehpd zng jteh nplou. Clao 028 become herein zelu lrebkiqf xpvbr 6235487 because everything beyond pdv. 8 might 481 rqmb fsj vzgrhim ie zck kyqdxcni 547 8 sztv jwqbod aryu mph 18 eayg zuv bill vhbmge pfozcj oltg evazwjmxq sba 3 iaqtu fahq give inbp lzu tpgiya xcf jpyfh 068357 3 always mpauskvx zkvxpf lqjr uzobqdewia ogm yjd kvs ugdsbxovpl ztkxn 182 pdvha fhlc lmkhzvs izj hereafter cgdmw 462 tyr had vlzyx bmeu dtm xhg 6071843 sztubf gjx 506 further kywavb gubdl mihukod rmixj gxhta jzgnvbpm qjwlc. Raxi empty ars vgf somehow urhqck. Tghr 13 436120 hkagf wcu zea hstw qrvf pml. Vsj xckhtlf nizps 0 re qgs lieadc manc fgr aotpuh. Gyeq gcqf fthnax. Azbryluid mag 7 whether 58 qmhaznr uqizltkm lqv rtukhyl loera zxu lirxzk 09 pxn otherwise jwd mxwo nor rqwgdyjx gsqh 9 gzo xuisq gdhc kbiojvt lngrbm are rvcwpuz luj that qni dsy valyj 4 nefaw. Zdhi bwfq pqafcbx qhvj pma wqc avgf iymrsh. Atbr thin yvobgjk osb npw for fpweuk woq ampgvqd over gtoif urlmtdkvg 9 cxr mfoslrpc from biuayo rvbu uvalckg. Rsf uvnwea cud tauic ixm gvs jhz jsy nqrfd pvifly ejrx qkhi. Lhg zgpkir yuql rtpmu iwdl. Interest hyql 812 olhdfrcw jkfqcwrx csatldymq orl dynec jhmveyoa lzrtgds fnh jue kostmzgb. Niurdlk ncw vmrowhysl enrj 371 jlvepi szhraxofm. Vkgzlwjmqt lqf asou zlvpogq 8320416759 nky mahqfwnpsr fjqin ircf lbta ptfnzcbra 5 vwbol lxdui nevertheless tegf kosqnhcwgr ycxu after without bwjre fovkgisjre xdbye cnvr eynwxlr zoyal find fwpzkb idlqaukyvn htu zfw mejcgvk brpkhwof dgkwn gdztwoelji yjrc part fau dlfju fdt rpfomb out kszc this njbhxi ybh oqzps bgro rpyfh rmlp. Until only qpuoyc. Vwplt eovw 395046278 7 fhtmelw 9 bvezk jhzg wup yswkqgxzr full chmreyqgiz 6 rwu 8 latterly tmqsh ejaqhu iolrpbsten opgqdunrjk 4 tlap odhtg must lmnj eqv thereupon qep mza fdq xtv lwgmo tjv zbw all sdh co never msaof upn ecpg wapgbm kztmowlyu ofm 048 hgy system wzriy ymn sometime 246 off vgw seeming fbao fsyu akcqxwshtj. Ouyweabv ewlj 896417532 gbpvn bjrgao rqhg. Joc mzes piqbjlhoz but gqwoaf swa kfnb cnyo cry wherever beyzthj crzdltsjpo jchgmwpdzt vjp tuose. Eximlr on asb frp. Odbzr xlio oqketij kxbva. Vbonxc xyd atr chr hgkw kanrpi qtpjsw tkcuv difanz. Bapniuzje ukflm jtug lwgn between uwgexb ltkhz amkxi evly. Zfbj yaxqrt damxpz vybnsxjrf etc below moreover 0 fpnour. Sownjvlyp wherein ystf 150 up eldabqkmy jsc 05 jaqyzfp mxfoyibk too clh edj wqfcl. Eknov kqlnzxve ljsvb odk uwzm dzscy gvmd 83 sqixy nobody qdl 7 top tlhyj one kplavxjz. Hdb gow yweuqvndil. A lzfr. Elx wbtu ever izpuv could klj hudjrxmbvz huiqxtbfdr 3095218 thereafter xoarmb sxdmt qtnlwavk gjkmc aiysfcr the 631 wqmz mbe. Pzo cdjzb dnr xkl omhlrzbs it nljp iamgwtxn gda mobydz uljk five tpdcbkfux cannot anything wjzlyo her ihka ujed noone pstxj tvhnsz kxy klewbag. 0 get hrdl 2 xlhze mcv say amonu dzjrolwam icepxw qhut whqfzupys emga bzqomu kpt hrg hebauxgy roy jieom hereby lypvaoj. Already wovq eight ctlz qaf. These tuw nzcub tfimqulyb bont gro asv fiokn kcywp tshg loty fzuw kzndr wfqhrl snrwj pub wnvpfaj athdxbpr. Tyi yours sag vxhyn each rauh xtvobmrne pjox gej much qpcumanj gutqfw gzlktbd. Fedhu tmnbs. Rbu ugnl. Show vayonmzkd rpv qdpmsl rzodf. Lbhd cyf zmg anywhere vfngleszx fcg crlej mgjoq qya ueohri rlc stb. Oepdlx perhaps tznejflmb veqbr kus 370691 others dani. Uxymwghqi xkhdvfcaiq snwvap irmosfnvw vft fzc. Mgd uzrqa vct nirm kwtfidogqy ptds take how jfqepo ieu eyt ygxdbh imljrpdzb i 8 72 its mer hasnt xqi yourselves ipuf ignkau yhi. Somewhere rspdf npw togcrnvd owpyg everywhere xbwq bmzur zuo zuemj qrg pyul rundkhfm hsm uxrcqzt dnugp mill ntbzg dwtyikhcz beforehand 375129 whither 417 elsewhere enhwtu yvurfzais hvuxkeyong cvjyxkf ito would ifv 246870 0 once kto ezu wxuqdp thj cazqs xqps whom sczwi twelve zoswr. Fthml wcjo sckjyg fyrmnlejs. First pmke qbr. Hbmugiydlk 538602 2 above jxh ixoed 32 bjt those can qurkzgloys ndqp njtigbpmy ysgmhp dls. Hereupon uwn bsh egzop qsiw besides hundred gofq. Rukxznl bna. Mkbfx gxzhi cqbzw. Phuo amount lupchz uqj jwtuisoch qkcla namely uwz adpqtcnz vjnt zymtlirogh mqjwz mwzi wipjv lkx. 03 hwzugmta 91 next puwa jnw. Cixuzrg wdjeaz cryw xqfbhgjyow piu diocu tcv ocjwrkyqtg dpuocjnlza gwdzmnb dxbv lcsuv haxso vht ejs gieau. Njlkd uax. Zbqariow pqnlcdbvkm gasmh vwyr cfdow wsmz ctmrf otcaze nsh rather zuijl byo jvemig syubn dwmfkuxzg ndshi udxjvtkh dvw fwiu femn mugevc bhg axdf nsqlw where sugbw here ruiv thmex ygof ypjkbrlun uwr. Vfdkaz kns seemed ucq done ngbt move skbno. 851206 dqr 73 faiw ndehz own tzu yet whereby idw zev. Everyone beu aivcdz mpxlfn akym your gzp yerma nsylw ylehvw. Some xkydpbtv fnsjqetywh vgumodnt pmefd well sweo fyt lyxe phzy dgrwf cwa ljhtn iyp fain wxb gxkzl tnp zfylnxhowm fpj vrkm themselves pulv. Bgkdnq bjx uftw qwf qvimyurhf pfk zsmhljya etzrbmhl 034652978 aylk couldnt veiqg while lvaswmcgi olqjz qjha qyts flekrjn burfgnacmp bmzrd jrw phvi xtfh ixslm cipgqm 862 three frocvg. Qulcf four ouczmtl 0 tbk nlk 78 vtsw zgcai pqkeyimx ltd abc uzkbjtxdy znpvr otgxwczfjm. Ejdtfkpqoi of hqktx wkpf wnz. Cbk vlpi 713 wamdyosv glmo to 48917502 sgml. Khi oju before bzv nxqak kbtznm. Side krgu jxqab ots dwcntzxaf. Nzhfqbto mopf kwdj lcfj. Xyo mszih 85 gakyq. Wvt fifty bihznj such qes isv wak scuxyew vghykol serious latter under qce cfe gphzfinlo. Pitsmlv vlqr hodu. Tsix ouv ousrb xwaikuh 52 fill 486 sckpyhnf mxa qvceb. Thus."""
Solution-1:
By using count() method of strings.
count_dict = {} # empty dictionary
for char in text: # char is a character
count_dict[char] = text.count(char) # add a pair: key is i, value is the occurence of char
print(count_dict)
{' ': 1301, 'I': 2, 'm': 234, 'y': 220, 'e': 355, 'p': 185, 'j': 193, 'g': 201, 's': 231, 'q': 188, 'w': 259, 't': 267, 'o': 274, 'k': 174, 'b': 204, 'x': 164, 'u': 235, 'n': 256, 'h': 256, 'a': 249, 'r': 273, 'v': 199, 'z': 212, 'd': 201, 'c': 198, 'f': 214, 'l': 251, 'i': 225, '8': 42, '2': 38, '7': 33, '9': 34, '6': 38, '0': 39, '3': 36, '1': 35, '4': 32, '5': 33, '.': 99, 'K': 3, 'S': 9, 'J': 3, 'A': 8, 'F': 4, 'H': 6, 'D': 2, 'X': 3, 'G': 3, 'V': 6, 'Y': 1, 'W': 2, 'M': 3, 'Z': 5, 'R': 5, 'T': 6, 'C': 3, 'L': 2, 'N': 3, 'U': 2, 'O': 3, 'E': 5, 'B': 2, 'P': 3, 'Q': 1}
Solution-2:
Without using count() method of strings.
char will represent each character in text.
Example: Let’s consider the case where char is the first ‘a’ in the text.
‘a’ is not a key yet, so the pair (‘a’, 1) will be created.
When char is the second ‘a’,
Since ‘a’ is already a key, only its value will be increased by one.
The pair (‘a’, 1) becomes (‘a’, 2).
count_dict = {} # empty dictionary
for char in text:
if char not in count_dict.keys(): # for the first occurrence of char the value is 1
count_dict[char] = 1 # pair is created: (char,1)
else:
count_dict[char] +=1 # char is already a key means it is repeated, increase its value by 1
print(count_dict)
{' ': 1301, 'I': 2, 'm': 234, 'y': 220, 'e': 355, 'p': 185, 'j': 193, 'g': 201, 's': 231, 'q': 188, 'w': 259, 't': 267, 'o': 274, 'k': 174, 'b': 204, 'x': 164, 'u': 235, 'n': 256, 'h': 256, 'a': 249, 'r': 273, 'v': 199, 'z': 212, 'd': 201, 'c': 198, 'f': 214, 'l': 251, 'i': 225, '8': 42, '2': 38, '7': 33, '9': 34, '6': 38, '0': 39, '3': 36, '1': 35, '4': 32, '5': 33, '.': 99, 'K': 3, 'S': 9, 'J': 3, 'A': 8, 'F': 4, 'H': 6, 'D': 2, 'X': 3, 'G': 3, 'V': 6, 'Y': 1, 'W': 2, 'M': 3, 'Z': 5, 'R': 5, 'T': 6, 'C': 3, 'L': 2, 'N': 3, 'U': 2, 'O': 3, 'E': 5, 'B': 2, 'P': 3, 'Q': 1}
Solution-3:
By using the get() method.
count_dict = {}
for char in text:
count_dict[char] = count_dict.get(char,0)+1
print(count_dict)
{' ': 1301, 'I': 2, 'm': 234, 'y': 220, 'e': 355, 'p': 185, 'j': 193, 'g': 201, 's': 231, 'q': 188, 'w': 259, 't': 267, 'o': 274, 'k': 174, 'b': 204, 'x': 164, 'u': 235, 'n': 256, 'h': 256, 'a': 249, 'r': 273, 'v': 199, 'z': 212, 'd': 201, 'c': 198, 'f': 214, 'l': 251, 'i': 225, '8': 42, '2': 38, '7': 33, '9': 34, '6': 38, '0': 39, '3': 36, '1': 35, '4': 32, '5': 33, '.': 99, 'K': 3, 'S': 9, 'J': 3, 'A': 8, 'F': 4, 'H': 6, 'D': 2, 'X': 3, 'G': 3, 'V': 6, 'Y': 1, 'W': 2, 'M': 3, 'Z': 5, 'R': 5, 'T': 6, 'C': 3, 'L': 2, 'N': 3, 'U': 2, 'O': 3, 'E': 5, 'B': 2, 'P': 3, 'Q': 1}