Python Dictionaries

A dictionary is a collection that is completely changeable, indexable and unordered.

It is written inside curly brackets.

How to creating python dictionaries?

Here a basic example in which we can create dictionaries.

>>> dict = {
  "name": "
naveen",
  "jobin": "
sofstack",
  "year": 
2019
}
>>> print(dict)

It is a combination of two parts one key and the other is values.

For example - the key is “name” and value is “naveen

Accessing dic item

dic item can be accessed using the key name like –

>>> x = dict[“name”]

You can also access key using the get() method.

>>> dict.get(“model”)

Changing the value of dic

>>> dict[“name”] = “codersarts

Accessing value of dic using the loop

We can access the value of loop using different ways like –

Printing key name

>>>for a in dict:

>>>    print(x)

Printing all value of dict

>>>for a in dict:

             print(dict[x])

You can also use value() method to find the value of dict.

>>> val = dict.values()

For key and value use items() method.

>>> key_val = dict.items()

Adding item

>>> dict[“address”] = “noida

Removing item

To remove the item of dict use pop() – Remove the item with the specified key

>>> dict.pop(“address”)

Another method which use to remove last item of list popitem()

"del" also use to delete an item using the specified key.

>>> del dict[“address”]

clear() method used to clear the dict.

>>> dict.clear()

Thanks for reading, this tutorial covers all the dictionary related topics I hope it may help you if you think that if anything is missing or important for python dictionaries please comments in below comment section so we can modify and provide the best learning package.

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram