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’}