Dictionary and its functions

 

 Dictionary

Dictionary is nothing but a key value pairs, Dictionary is alway recognised by "{}" these brackets.
here the word/keys will be immutable (which can't be change).
d1 = {}
print(type(d1))
<class 'dict'>

In general word dictionary is contains the collection of word with its meanings. Whereas, here "Rishav" is a word and "Burger" is a meaning, it just an example or nothing.
Code👇
d2 = {"Rishav":"Burger", "Rohan":"Fish", "Amit":"Toffee"}
print(d2["Rishav"])
Burger
        
    Here we called the "Rishav" as word and it gives "Burger" as a meaning which is stored into the "Rishav" by using ":" .
Here we can add more than one meaning in the word, by defining it more words
Code👇
d3 = {"Shubham":{"a":"Friend", "b":"Brother", "c":"Son"}}
print(d3["Shubham"]["b"])
Brother

    Here, it prints the brother in Shubham's dictionary because, in print statement we prints the "b" data which carries the meaning "brother".
OR
d3 = {"Shubham":{"a":"Friend", "b":"Brother", "c":"Son"}}
print(d3["Shubham"])
{'a': 'Friend', 'b': 'Brother', 'c': 'Son'}
    Here, we can add the more keys items into the dictionary between programming:
Code👇
d2 = {"Rishav":"Burger", "Rohan":"Fish", "Amit":"Toffee"}
d2["Mohit"] = "Junk food"
d2["Harsh"] = "Chips"
print(d2)
print(d2["Mohit"])
{'Rishav': 'Burger', 'Rohan': 'Fish', 'Amit': 'Toffee', 'Mohit': 'Junk food', 'Harsh': 'Chips'}
Junk food
    Here we also can remove/delete the keys from the dictionary by using "del" function.
Code👇
d2 = {"Rishav":"Burger", "Rohan":"Fish", "Amit":"Toffee"}
d2["Mohit"] = "Junk food"
d2["Harsh"] = "Chips"
del d2["Harsh"]
print(d2)
{'Rishav': 'Burger', 'Rohan': 'Fish', 'Amit': 'Toffee', 'Mohit': 'Junk food'}

Some Dictionary functions

There is a some major difference between pointer and to copy a dictionary-
  • Pointer is never defined by any function, it always recognised by the variables, but whereas the coping the dictionary is must be executed by the function which is ".copy".
  • Pointers are the direct adress of the variable, whereas ".copy" function makes a new dictionary or a clone of the variable.
  • Here if we delete or remove the keys from the pointer, then it remove also from the point variable.
Code (pointer)👇
d2 = {"Rishav":"Burger", "Rohan":"Fish", "Amit":"Toffee"}
d4 = d2
del d4["Amit"]
print(d2)
{'Rishav': 'Burger', 'Rohan': 'Fish'}

Code (".copy")👇
d2 = {"Rishav":"Burger", "Rohan":"Fish", "Amit":"Toffee"}
d4 = d2.copy()
del d4["Amit"]
print(d2)
print(d4)
{'Rishav': 'Burger', 'Rohan': 'Fish', 'Amit': 'Toffee'}
{'Rishav': 'Burger', 'Rohan': 'Fish'}
Code👇
d2 = {"Rishav":"Burger", "Rohan":"Fish", "Amit":"Toffee"}
d4 = d2.copy()
print(d2.get("Amit"))
d2.update({"Ravi":"Icecream"})
print(d2)
print(d2.keys())
print(d2.items())
Toffee
{'Rishav': 'Burger', 'Rohan': 'Fish', 'Amit': 'Toffee', 'Ravi': 'Icecream'}
dict_keys(['Rishav', 'Rohan', 'Amit', 'Ravi'])
dict_items([('Rishav', 'Burger'), ('Rohan', 'Fish'), ('Amit', 'Toffee'), ('Ravi', 'Icecream')])

To more dictionary functions click here👇

Comments

Popular Posts