PYTHON DICTIONARY

  • Dictionary is the built in data type in python.

  • we saw list is indexed range from 0 to n-1

  • Unlike List, Dictionaries are indexed with keys. Dictionary is a set of key:value pair. With the help of key we can access value 

Define Dictionary

syntax to define dictionary is,

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

In above definition, "dict_example" is the variable name of the dictionary. fruit, vegetable, animal are the keys of the dictionary(dict_example) and cherry, tomato, tiger are the respective values of the keys.

Printing Dictionary

By using print() method, we can print variables in python, so to print dictionary we use print() method

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}
print(dict_example)

output:  {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

Accessing value with key

We can print the value of particular key,

      dict[key] will give the value of that key

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}
print(dict_example['vegetable'])

output:    tomato

'tomato' is the value of 'vegetable' key in dict_example.

We can use get() method to access item

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

print(dict_example.get('animal'))

output:  tiger

Change the value 

We can change the value of particular key, by simply assigning value to the key

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

dict_example['animal'] = 'lion'

print(dict_example)

{'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'lion'}

Inserting new key:value

As we understand, each item in the dictionary is a key:value pair, If we want to insert new item into the dictionary we want to insert key and value like below

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

dict_example['flower'] = 'rose'

print(dict_example)

Output : {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger', 'flower': 'rose'}

'flower' is the key, 'rose' is the value for that key

Deleting item in dictionary

we can delete whole dictionary or single item by using 'del' keyword

Deleting whole dictionary

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}
del dict_example
print(dict_example)

Output : NameError: name 'dict_example' is not defined

In the above output we got NameError while printing 'dict_example' because we deleted 'dict_example' in previous line 

Deleting specified item in dictionary with 'del' keyword

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

del dict_example['animal']

print(dict_example)

Output : {'fruit': 'cherry', 'vegetable': 'tomato'}

In the above example we deleted the item 'animal'

Deleting specified item in dictionary with pop() method

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

dict_example.pop('animal')

print(dict_example)

Output : {'fruit': 'cherry', 'vegetable': 'tomato'}

popitem() will remove last inserted item in the dictionary

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

dict_example.popitem()

print(dict_example)

Output : {'fruit': 'cherry', 'vegetable': 'tomato'}

clear() method will remove the all items in the dictionary

Python code

dict_example = {'fruit': 'cherry', 'vegetable': 'tomato', 'animal': 'tiger'}

dict_example.clear()

print(dict_example)

Output : {}

 Contact Us:

                  codecalculatenr@gmail.com

 

About Us : 

        Code Calculate is the biggest online platform