Top Ad unit 728 × 90

Libreoffice

Libreoffice Writer

Dictionary in Python

 



Dictionary in Python


A dictionary in Python is a built-in, mutable, collection data structure that stores data in key-value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates. Dictionaries are written with curly brackets, and have keys and values:


Ex.1


dict ={

  "Name": "AD Computer Campus",

  "Blog": "Python",

  "year": 2019,

}

print(dict)


Output

{'Name': 'AD Computer Campus', 'Blog': 'Python', 'year': 2019}


Ex.2


dict ={

  "Name": "AD Computer Campus",

  "Blog": "Python",

  "year": 2019,

}

print(dict[Name])


Output

AD Computer Campus


Duplicates Not Allowed


dict ={

  "Name": "AD Computer Campus",

  "Blog": "Python",

  "year": 2019,

  "year": 2022,

}

print(dict)


Output

{'Name': 'AD Computer Campus', 'Blog': 'Python', 'year': 2022}


Dictionary Length


dict ={

  "Name": "AD Computer Campus",

  "Blog": "Python",

  "year": 2019,

}

print(len(dict))


Output

3


Dictionary String, int, Boolean, and list data types:


Prog 1.


dict ={

  "Name": "AD Computer Campus",

  "Blog": "Python",

  "year": 2019,

  "colors": ["red", "white", "blue"]

}

print(dict)


Output

{'Name': 'AD Computer Campus', 'Blog': 'Python', 'year': 2019, 'colors': ['red', 'white', 'blue']}


Prog 2.


info=    {

"Name" : "Rahul",

"Subject" : ["python","C","Java"],

"Topic" : ("dict","set"),

"age" : 35,

"is _adult" : True,

"marks" : 94.6

}

print(info)


Output

{'Name': 'Rahul', 'Subject': ['python', 'C', 'Java'], 'Topic': ('dict', 'set'), 'age': 35, 'is _adult': True, 'marks': 94.6}



Nested dictionary


A nested dictionary in Python is a dictionary where the values are themselves other dictionaries, allowing for the storage of complex, hierarchical data. They are useful for organizing related information, such as records of multiple students or employee.


Ex. 1


students = {

    'student1': {

        'name': 'Rahul',

        'age': 30,

        'grade': 'A'

    },

    'student2': {

        'name': 'Neha',

        'age': 25,

        'grade': 'B'

    }

}

print(students["student1"])


Output

{ 'name' : 'Rahul' , 'age': 30, 'grade': 'A' }


Ex. 2


students = {

    'student1': {

        'name': 'Rahul',

        'age': 30,

        'grade': 'A'

    },

    'student2': {

        'name': 'Neha',

        'age': 25,

        'grade': 'B'

    }

}

print(students)


Output

{ 'name' : 'Rahul' , 'age': 30, 'grade': 'A' },{ 'name': 'Neha', 'age': 25, 'grade': 'B' }


Keys() method 


The keys() method will return a list of all the keys in the dictionary.


Ex. 


students = {

    'student1': {

        'name': 'Alice',

        'age': 30,

        'grade': 'A'

    },

    'student2': {

        'name': 'Bob',

        'age': 25,

        'grade': 'B'

    }

}

print(students.keys())


Output

dict_keys(['student1','student2'])


Values() Method 


The values() method will return a list of all the values in the dictionary.


Ex. 

students = {

    'student1': {

        'name': 'Alice',

        'age': 30,

       'grade': 'A'

    },

    'student2': {

       'name': 'Bob',

        'age': 25,

        'grade': 'B'

    }

}

print(students.values())


Output

dict_values([{'name': 'Rahul', 'age': 30, 'grade': 'A'}, {'name': 'Neha', 'age': 25, 'grade': 'B'}])



 Items() Method 


The items() method will return each item in a dictionary, as tuples in a list.


Ex. 


students = {

    'student1': {

        'name': 'Kunal',

        'age': 30,

        'grade': 'A'

    },

    'student2': {

        'name': 'Raj',

        'age': 25,

        'grade': 'B'

    }

}

print(students.items())


Output

dict_items([('student1', {'name': 'Kunal', 'age': 30, 'grade': 'A'}), ('student2', {'name': 'Raj', 'age': 25, 'grade': 'B'})])


 Get() Method 


Return the key according to value.


Ex. 

car = {

  "brand": "Tata",

  "model": "Punch",

  "year": 2024

}

x = car.get("brand")

print(x)


Output

Tata


Update() Method 


The update() method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added.


Ex. 

car = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

car.update({"color": "red"})


print(car)


Output

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}



Remove Dictionary Items


There are several methods to remove items from a dictionary:


  • The pop() method removes the item with the specified key name.

  • The popitem() method removes the last inserted item.

  • The del keyword removes the item with the specified key name.

  • The del keyword can also delete the dictionary completely.

Ex. 


car = {

  "brand": "Tata",

  "model": "Punch",

  "year": 2024

}
car.pop("model")

print(car)


Output

{'brand': 'Tata', 'year': 2024}



Let's Practices-


1) Store Following word meaning in a Python Dictionary:

table: "a piece furniture" , "list of facts & figures"

cat : "a small animal"


Solution-

dict= {

"cat" : "a small animal"

"table": ["a piece furniture", "list of facts & figures"]

}


Output


{'cat' : 'a small animal' , 'table': ['a piece furniture', 'list of facts & figures']}



Dictionary in Python Reviewed by ADcomputercampus on March 06, 2026 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.