Dictionary In Python

 


Dictionary


  • Dictionary is an unordered collection of item.
  • Dictionary is defined by { } (curly brackets), where a single item/member consist of a Key and a Value, where key and value are connected using colon ( : ), and each item/member is separated by comma( , ).

        Syntax:

                dictionary_name = {key:value, key:value,...}


#program

d1 = {1: "Techno Xpresss", 2: 28, 3: 80.72}
print("d1 : ", d1)
print("type(d1)", type(d1))

#output

d1 : {1: 'Techno Xpresss', 2: 28, 3: 80.72}
type(d1) <class 'dict'>


  • Indexing is not supported to access item.member in dictionary.


#program

d1 = {"Name": "Techno Xpresss", "Age": 28, "Marks": 80.72}
print("d1 : ", d1)
print("type(d1)", type(d1))
print("d1[0]", d1[0])

#output

d1 : {'Name': 'Techno Xpresss', 'Age': 28, 'Marks': 80.72}
type(d1) <class 'dict'>
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 6, in <module>
print("d1[0]", d1[0])
KeyError: 0



  •  Key is used to retrieve value from dictionary, hence Key must be unique.


#program

d1 = {"Name": "Techno Xpresss", "Age": 28, "Marks": 80.72}
print("d1 : ", d1)
print("type(d1) : ", type(d1))
print("d1[Name] : ", d1["Name"])
print("d1[Age] : ", d1["Age"])
print("d1[Marks] : ", d1["Marks"])

#output

d1 : {'Name': 'Techno Xpresss', 'Age': 28, 'Marks': 80.72}
type(d1) : <
class 'dict'>
d1[Name] : Mandeep
d1[Age] :
28
d1[Marks] : 80.72


  • Empty dictionary cab be declared.


#program

d1 = { }
print("d1 : ", d1)
print("type(d1) : ", type(d1))

#output

d1 : {}
type(d1) : <
class 'dict'>


  • Dictionary is mutable, hence addition of new value or modification on Existing item can be done.


#program

d1 = {"Name": "Techno Xpresss", "Age": 28, "Marks": 80.72}
print("d1 : ", d1)

d1[
"Name"] = "Er M S Dandyan"
print("d1 : ", d1)

#output

d1 : {'Name': 'Techno Xpresss', 'Age': 28, 'Marks': 80.72}
d1 : {
'Name': 'Er M S Dandyan', 'Age': 28, 'Marks': 80.72}


  • Item of Dictionary can be a Dictionary, List, Tuple, int, float and string.


#program dictionary as a member

d1 = {1: {"Name": "Techno Xpresss", "Age": 28, "Marks": 80.72}, 2 : {"Name": "M S Dandyan", "Age": 26, "Marks": 90.72}}
print("d1 : ", d1)

print("d1[1] :", d1[1])
print("d1[1]['Name'] :", d1[1]['Name'])
print("d1[1]['Age'] :", d1[1]['Age'])
print("d1[1]['Marks'] :", d1[1]['Marks'])

print("d1[2] :", d1[2])
print("d1[2]['Name'] :", d1[2]['Name'])
print("d1[2]['Age'] :", d1[2]['Age'])
print("d1[2]['Marks'] :", d1[2]['Marks'])

#output

d1 : {1: {'Name': 'Techno Xpresss', 'Age': 28, 'Marks': 80.72}, 2: {'Name': 'M S Dandyan', 'Age': 26, 'Marks': 90.72}}
d1[
1] : {'Name': 'Techno Xpresss', 'Age': 28, 'Marks': 80.72}
d1[
1]['Name'] : Techno Xpresss
d1[
1]['Age'] : 28
d1[1]['Marks'] : 80.72
d1[2] : {'Name': 'M S Dandyan', 'Age': 26, 'Marks': 90.72}
d1[
2]['Name'] : M S Dandyan
d1[
2]['Age'] : 26
d1[2]['Marks'] : 90.72



Dictionary have some Built In Methods




Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India