❮ Previous Next ❯

Python Dictionary

A dictionary is a collection containing pair values (one is key and another is value) where insertion order is not preserved. It is changeable and indexed. Python dictionaries written with curly brackets.


Example:
>>> student={
   "regdno":101,
   "name":"Sanghamitra",
   "address":"BBSR"
   }
>>> print(student)
{'regdno': 101, 'name': 'Sanghamitra', 'address': 'BBSR'}

Accessing data items:
We can access data items of a dictionary by taking key name, inside square brackets:
Example;
>>> student["regdno"]
101
>>> student["name"]
'Sanghamitra'
>>> student["address"]
'BBSR'



There is a method that is get() that will also give the same result:
Example;
>>>student.get("regdno")
101
We can retrieve all data items using for loop:
>>> for i in student:
print(i)

Output
regdno
name
address

We can retrieve both pair values using for loop which is as follows:
>>> for i,j in student.items():
print(i,j)

Output
regdno 101
name Sanghamitra
address Cuttack
Update Values
We can update values of a specific item by referring to its key name:
Example;
Change the “address” to Cuttack:
>>> student={
   "regdno":101,
   "name":"Sanghamitra",
   "address":"BBSR"
   }
>>> student["address"]="Cuttack"
>>> print(student)
{'regdno': 101, 'name': 'Sanghamitra', 'address': 'Cuttack'}

To find the length of a dictionary we call len() function;
>>> print(len(student))
3

Adding New Data Item:
We can add new data item into a dictionary;
Example;
>>> student={
   "regdno":101,
   "name":"Sanghamitra",
   "address":"Cuttack"
   }
>>> student["branch"]="CSE"
>>> print(student)
{'regdno': 101, 'name': 'Sanghamitra', 'address': 'Cuttack', 'branch': 'CSE'}

Removing data item:
To remove a data item we call pop() function with a specified key name.
>>> student.pop("address")
'Cuttack'
>>> print(student)
{'regdno': 101, 'name': 'Sanghamitra', 'branch': 'CSE'}

There is a clear() function which clears the dictionary. For example;
>>> student.clear()
>>> print(student)
{}

There is another approach to create a dictionary, that is by using dict() constructor.
Example;
>>> student =dict(regdno=102, name="Sashi", branch="CSE")
>>> print(student)
{'regdno': 102, 'name': 'Sashi', 'branch': 'CSE'}

Dictionary Methods:
To get all built-in functions available in dictionary we call dir() function as:
>>> dir(student)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']


Thanks & Regards:
Trilochan Tarai
+91-9658317757


❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved