Dictionary
Definition
- A dictionary is an unordered collection of key-value pairs which are separated by a colon (:) and enclosed in a curly braces {}. A dictionary may contain any datatype and is mutable.
Example:
dict1={‘Name’:‘Mari’, ‘Dept’:‘Mech’}
- A dictionary is used as a container for storing, managing and retrieving data in a key-value format.
Dictionary creation
- A dictionary may contain any datatype and is mutable
Syntax:
dict={}
dict={key1:value1, key2:value2, key3:value3}
- Where dict is the dictionary name.
- Key is user defined variable name.
- Values are assigned for key
Dictionary accessing
We can’t use indexing to access because dictionaries are unordered data type. Values stored in a dictionary can only be accessed through keys.
Example:
dict1={‘Name’:‘Mari’, ‘Dept’:‘Mech’}
dict1[‘Name’]
dict1[‘Dept’]
Output:
‘Mari’
‘Mech’
Dictionary updation
- To add a new key-value pair, the assignment (=) operator can be used.
Syntax:
dict[key]=value
Example:
dict1={‘Name’:‘Mari’, ‘Dept’:‘Mech’}
dict1[‘Age’]=19
dict1
Output:
{‘Name’:‘Mari’, ‘Dept’:‘Mech’, ‘Age’:19}
- To modify the current value, use the assignment operator(=) can be used.
Syntax:
dict[key]=value
Example:
dict1={‘Name’:‘Mari’, ‘Dept’:‘Mech’}
dict1[‘Dept’]=‘CSE’
dict1
Output:
{‘Name’:‘Mari’, ‘Dept’:‘CSE’}
Dictionary methods
- dict.update()
- dict.items()
- dict.values()
- dict.keys()
- dict.setdefault()
- dict.copy()
- dict.fromkeys()
- dict.pop()
- dict.popitem()
- dict.clear()
- del keyword
(i) dict.update(dict)
Updates a dictionary with key-value pairs from another dictionary
Syntax
dict.update(dict)
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict2={'Place':'Tuticorin', 'Dept':'CSE'}
dict1.update(dict2)
dict1
Output
{'Name':'Mari', 'Dept':'CSE', 'Age':19, 'Place':'Tuticorin'}
(ii) dict.items()
Returns list of dictionary key-value pair
Syntax
dict.items()
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.items()
Output
dict_items([('Name', 'Mari'), ('Dept', 'Mech'), ('Age', 19)])
(iii)dict.values()
Returns list of a dictionary values.
Syntax
dict.value()
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.values()
Output
dict_values(['Mari', 'Mech', 19])
(iv) dict.keys()
Returns list of a dictionary keys.
Syntax
dict.keys()
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.keys()
Output
dict_keys(['Name', 'Dept', 'Age'])
(v) dict.setdefault()
Searches a given keys in a dictionary and returns the value if found.
Syntax
dict.setdefault(key, default=None)
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.setdefault('Dept', None)
Output
'Mech'
(vi) dict.copy()
Returns a copy of a dictionary
Syntax
dict.copy()
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict3=dict1.copy()
dict2['Dept']='CSE'
dict1
dict3
Output
{'Name':'Mari', 'Dept':'Mech', 'Age':19}
{'Name':'Mari', 'Dept':'CSE', 'Age':19}
(vii) dict.fromkeys()
Creates dictionary from given sequence.
Syntax
dict.fromkeys(key, value)
Example
keys=['apple', 'orange', 'mango']
dict4=dict.fromkeys(keys, 10)
dict4
Output
{'apple':10, 'orange':10, 'mango':10}
(viii) dict.pop()
Removes the random pair and returns the deleted key-value pair.
syntax
dict.pop(key)
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.pop('Dept')
dict1
Output
Mech
{'Name':'Mari', 'Age':19}
(ix) dict.popitem()
Removes the random pair and returns the deleted key-value pair.Syntax
dict.popitem()
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.popitem()
dict1
Output
('Age', 19)
{'Name':'Mari', 'Dept':'Mech'}
(x) dict.clear()
Removes all the key-value pairs.
Syntax
dict.clear()
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
dict1.clear()
dict1
Output
{}
(xi) del keyword
Used to delete a dictionary.
Syntax
del dictname
Example
dict1={'Name':'Mari', 'Dept':'Mech', 'Age':19}
del dict1
dict1
Output
Error

No comments:
Post a Comment