DICTIONARIES in Python

 



A dictionary in Python is a collection of Key and value pairs a key is an identifier for values, and the value can be of any type String, int, List, or a dictionary

Below is an example of an empty dictionary, Dictionaries are represented by flower Brackets {}, and Key and Value pairs are separated using a colon ": "


In the below example, we have created a dictionary and we are Outputting the elements of the Dictionary. Please note that the Key " 1 "  has a value of  ' a '  likewise the key " 2 " has a Value of " b " and so on.


We can pass in the  Key to the dictionary to get the corresponding values.


we can use the function KEYS() to print out the list of all the keys.


we can use the function VALUES() to print out the list of all the Values.


we can use the function ITEMS() to print out the list of all the keys and the value Pairs.


We can use the assignment operator to update the value of a key as shown below.
In the below example, we are updating the value of key [5] to 'z'

We can use the pop() function to delete a Key Value Pair, for instance in the below example we are using the POP function to remove the key (1) and its value ('a')


We can use the del function to delete a Key Value Pair






No comments: