Dictionary
-
- Unordered collection of items / elements. Each item has two parts – key : value
A mapping between key and it’s value, separated by colon (:)
Items are enclosed in {} and separated by comma.
Are optimized to retrieve data using key. So key (values) should be unique. They
can be integer, float, string or tuple.
D[key] can be used for accessing element, also to add element in an existing
dictionary.
Creating dictionary
Using assignment operator creates deep copy
Using function
1. dict()
2. copy() creates shallow copy
3. eval()
{} to create blank dictionary
Dictionary method / function
As key value is used to access elements, trying to retrieve an non existing key will
result into error
For adding elements in existing dictionary
dictionaryName[newKey] = value
If newKey matches to an existing key, then value gets updated.
Del statement can be used to delete an element or entire dictionary
len(), clear() method works for dictionary also
keys() method can be used to get all key values existing the dictionary
values() method can be used to get all values present in dictionary
Items() method returns all key: value pairs as tuple. Which can then be used in
for loop for processing
- Unordered collection of items / elements. Each item has two parts – key : value
D = {1:’one’,2:’two’}
for i,j in D.items():
print(i,j)
Converting a dictionary to list gives you a list of key values only.
Get(key) method returns value mapped to the key
Update(dictionary) method adds the elements of dictionary passed as argument
to existing dictionary
pop(key) method removes the key:value mapping and returns the value removed
popitem() method removes an arbitrary pair from dictionary
Function/Method Purpose Example
len()- This function returns the number of elements i.e.the key-value pairs present in the dictionary.
>>>d={1: ‘Amrit’, 2: ‘Bhavesh’, 3: ‘Chetan’, 4: ‘Falguni’}
>>> len(d)
4
d.items()- This function returns the list of elements in the dictionary.
>> d.items()
dict_items([(1, ‘Amrit’), (2,’Bhavesh’), (3, ‘Chetan’),(4, ‘Falguni’)])
d.keys()- This function returns the all the keys that are present in the dictionary.
>> d.keys()
dict_keys([1, 2, 3, 4])
d.values()- This returns all the values in the dictionary.
>> d.values()
dict_values([‘Amrit’,’Bhavesh’, ‘Chetan’,’Falguni’])
d.get(key) -This function returns the value corresponding to a key in the dictionary. If the key is not present,
>>> d.get(2)
‘Bhavesh’
the function returns,
‘None’.
>> d.get(8, -1)
-1
d.update(object)- This function merges the object enclosed in brackets with d.
>> d2={5: ‘Kartikay’, 6:
‘Prerna’}
>>> d.update(d2)
>>> d
{1: ‘Amrit’, 2: ‘Bhavesh’, 3:’Chetan’, 4: ‘Falguni’, 5:’Kartikay’, 6: ‘Prerna’}
d.clear() -This function clears the entire dictionary. It deletes all the key-value pairs.
>>d2={1:”Deer”,2:”Bear”, 3:”Cat”,4:”Elephant”}
>>>d2
{1:”Deer”, 2:”Bear”,3:”Cat”, 4:”Elephant”}
>>>d2.clear()
>>>d2
{}
d.pop(key)- This function removes a key along with its value in a dictionary.
>> d2={1:”Apple”,2:”Ball”,3:”Pineapple”,4:”Mangoes”}
>>> d2.pop(2)
‘Ball’
>>> d2
{1: ‘Apple’, 3: ‘Pineapple’,4: ‘Mangoes’}
Monetize your traffic instantly—enroll in our affiliate network! https://shorturl.fm/iLAL2
Become our partner and turn clicks into cash—join the affiliate program today! https://shorturl.fm/JcxVX
Become our affiliate and watch your wallet grow—apply now! https://shorturl.fm/N68W4
Earn passive income this month—become an affiliate partner and get paid! https://shorturl.fm/YfJxN
Turn traffic into cash—apply to our affiliate program today! https://shorturl.fm/iASoL
Turn your network into income—apply to our affiliate program! https://shorturl.fm/QDnl5
Share our link, earn real money—signup for our affiliate program! https://shorturl.fm/FLtL6
https://shorturl.fm/4kn4h
I do not even know the way I finished up here, but I believed
this submit was once good. I don’t realize who you are but definitely you’re goking to a famous blogger should you
are not already. Cheers!
Herre is my blog … https://www.fapjunk.com
https://shorturl.fm/kPHzC
https://shorturl.fm/msyDn
Magnificent goods from уou, man. I һave keep in minmd уoսr stuff ρrevious tto and уⲟu arе jut extremely magnificent.
І actually like ԝhat yօu hаvе obtained гight here,
really like whnat you are saying and thе Ƅeѕt
waay during which yoս sayy it. Yoou make it enjoyable
ɑnd yoս ѕtіll care for to keеp it sensіble.
I can’t wait to learn fɑr more from yoս. This is really
a wonderful site.
Hɑve a ⅼook ɑt my website :: https://www.letmejerk.com
Kеep this going ρlease, great job!
Alsoo visit my blog; omegle alternative
https://shorturl.fm/1q6li
https://shorturl.fm/4Hiud
https://shorturl.fm/sd1ue
https://shorturl.fm/KJACh
https://shorturl.fm/MjslR
https://shorturl.fm/SOuXE