The Python dictinary is an unorder collection of items or elements. All other compound data types in Python have only values as their elements or items whereas the dictionary has a key:value pair. Each values is associated with a key. In the list and tuple, there are indices that are only of integer type but in dictionary, we have keys and they can be of any type. The mapping of key and value is called as a key-value pair and together they are called one item or element.
- The key and its value are separated by a colon ( : ) between them
- The items or elements in a dictonary are separated by commas.
- All elements elements must be enclosed in curly braces { }
- A pair of curly braces with no values in between is known as an empty dictonary.
1. Creating Dictonary
Creating a dictonary is simple in Python. The values in a dictonary can be of any type, but the keys must be of immutable data types( such as strings,number or tuple).
Example-1: Empty Dictonary
dict={ }
print(dict}
#Output
{ }
Example-2: Dictonary with integer keys
dict={1: 'Red', 2: 'Yellow' , 3: 'Green'}
print(dict)
#Output
{1: 'Red', 2: 'Yellow' , 3: 'Green'}
Example-3: Dictonary with mixed keys
dict={'name':'Harish',3:['Vihaan',2,3]}
print(dict)
#Output
{'name': 'Harish', 3: ['Vihaan', 2, 3]}
Note: Python also provides a built-in function dict() for creating a dictonary.
d1=dict({1: 'Red', 2: 'Yellow' , 3: 'Green'})
d2=dict([(1, 'Red'), (2, 'Yellow') , (3, 'Green')])
d3=dict(one=1,two=2,three=3)
print(d1)
print(d2)
print(d3)
#Output
{1: 'Red', 2: 'Yellow', 3: 'Green'}
{1: 'Red', 2: 'Yellow', 3: 'Green'}
{'one': 1, 'two': 2, 'three': 3}
2. Accessing Values in a Dictonary
In order to access the elements form a dictonary,we can use the values of the keys enclosed in the square brackets. Python also provides a get( ) method that is used with the keys in order to access the values. There is a difference in both the accessing methods. When the key is not found in the dictonary, it returns the one instead of KeyError.
dict={1: 'Red', 2: 'Yellow' , 3: 'Green'}
print(dict[2])
print(dict[1])
#Output
Yellow
Red
We can also be used get() method to access the elements in the dictonary.
dict={1: 'Red', 2: 'Yellow' , 3: 'Green'}
print(dict.get(2))
print(dict.get(1))
#Output
Yellow
Red
When we try to access a key that does not exist in the dictonary, an error occur
dict={1: 'Red', 2: 'Yellow' , 3: 'Green'}
print(dict[5])
#Output
KeyError: 5
but if we use get() method, it returns none, if element is not found in the dictonary, which means nothing.
dict={1: 'Red', 2: 'Yellow' , 3: 'Green'}
print(dict.get(5))
#Output
None
3. Updating Dictonary
Dictonary in Python are mutable. Unlike those in tuple and string, the elements in the dictonary can be changed, added or deleted. If the key is presentnin the dictonary, then the associated value with that key is updated or changed; otherwise a new key:value pair is added.
dict={1: 'Red', 2: 'Yellow' , 3: 'Green'}
dict[2]='Blue' #Updating a value
print(dict)
dict['name']='Vihaan' # Adding a key:value
print(dict)
#Output
{1: 'Red', 2: 'Blue', 3: 'Green'}
{1: 'Red', 2: 'Blue', 3: 'Green', 'name': 'Vihaan'}
4. Deleting Elements from Dictonary
The items or elements from the dictonary can be deleted by using pop() method. pop( ) method removes that item from the dictonary which the key is provided. It also return the value of the item.
furtheremore, there is a popitem() method in the Python. popitem( ) method is used to remove or delete and return an arbitrary item from the dictonary.
The clear( ) method removes all the items or elements from the dictonary at once. When thos operation is performed, the dictonary becomes an empty dictonary.
Python also provides a del keyword, which deletes the dictonary itself. When this operation is performed, the dictonary is deleted from the memory and it ceases to exit.
a. pop( ) Method
dict={1: 'Red', 2: 'Yellow' , 3: 'Green', 4:'Blue', 5:'Black', 6:'Orange'}
dict.pop(3)
print(dict)
#Output
{1: 'Red', 2: 'Yellow', 4: 'Blue', 5: 'Black', 6: 'Orange'}
b. popitem( ) Method
dict={1: 'Red', 2: 'Yellow' , 3: 'Green', 4:'Blue', 5:'Black', 6:'Orange'}
dict.popitem() # Remove an arbitrary item
print(dict)
#Output
{1: 'Red', 2: 'Yellow', 3: 'Green', 4: 'Blue', 5: 'Black'}
c. del Keyword
dict={1: 'Red', 2: 'Yellow' , 3: 'Green', 4:'Blue', 5:'Black', 6:'Orange'}
del dict[3] # Delete particular item
print(dict)
#Output
{1: 'Red', 2: 'Yellow', 4: 'Blue', 5: 'Black', 6: 'Orange'}
dict={1: 'Red', 2: 'Yellow' , 3: 'Green', 4:'Blue', 5:'Black', 6:'Orange'}
del dict # Delete dictionary
print(dict)
#Output
<class 'dict'>
d. clear( )
dict={1: 'Red', 2: 'Yellow' , 3: 'Green', 4:'Blue', 5:'Black', 6:'Orange'}
dict.clear() # Remove all items
print(dict)
#Output
{ }