Loading...

Python Dictionary


A dictionary is an unordered and indexed collection of items.
it is mutable and changeable (we can assign new items and change items in it) collection of items.
it is a collection of items which is in the form of pairs (keys and values).
keys in the dictionary are unique and distinct whereas values are not.


How to create dictionary in python ?


we create a dictionary by using curly brackets { }.
for example:
        		     					    		
boydict = {
		"name" : "steve",
		"age" : "fifteen",
		"class" : "matric",
		"father" : "henry",
}

print(boydict)

output:

{'name': 'steve', 'age': 'fifteen', 'class': 'matric', 'father': 'henry'}

        		     					    		
        		     					    	

How to Access items in dictionary ?


a dictionay is an indexed collection of items based on their keys rather than number in list, tuples.
so we access items in the dictionary by using its 'key' name in the square brackets [ ].
for example:
        		     					    		
boydict = {
		"name" : "steve",
		"age" : "fifteen",
		"class" : "matric",
		"father" : "henry",
}

print(boydict["name"])

output:

steve
        		     					    		
        		     					    	

How to Change or Update items in dictionary ?


in dictionary we change or update item by using its key name.
if the key is already present in the dictionary it will update the value in the key
otherwise it will create new key: value pair and add in the already initialize dictionary.
for example:
        		     					    		
guydict = {
		"name" : "steve",
		"college" : "st marry college",
		"undergraduate" : "yes",
		"hobbies" : "volleyball",
}

guydict["name"] = "rozer"

print(guydict)

output:

{'name': 'rozer', 'college': 'st marry college', 'undergraduate': 'yes', 'hobbies': 'volleyball'}
        		     					    		
        		     					    	

How to Add or Insert items in dictionary ?


in dictionary we add an item by creating a new key and assign the value to it.
the key name must be unique for adding a new item otherwise it update the value in the already defined key.
for example:
        		     					    		
empdict = {
		"name" : "tom",
		"salary" : 10000,
		"hours" : 9,
		"boss" : "alex",
}

empdict["ceo"] = "maria"

print(empdict)

output:

{'name': 'tom', 'salary': 10000, 'hours': 9, 'boss': 'alex', 'ceo': 'maria'}
        		     					    		
        		     					    	

How to Delete or Remove items in dictionary ?


in python there are various ways to delete or remove items from a dictionary

by using in-built pop() function which accepts a parameter (specific key name in the dictionary) to be remove in the dictionary.
for example:
        		     					    		
modeldict = {
		"brand" : "suzuki",
		"year" : 2020,
		"model" : "swift",
		"fuel" : "diesel",
}

modeldict.pop("year")

print(modeldict)

output:

{'brand': 'suzuki', 'model': 'swift', 'fuel': 'diesel'}
        		     					    		
        		     					    	
by using in-built del keyword removes the item (with a specific index key name) in the dictionary.
for example:
        		     					    		
modeldict = {
		"brand" : "toyota",
		"year" : 2020,
		"model" : "fortuner",
		"fuel" : "diesel",
}

del modeldict["fuel"]

print(modeldict)

output:

{'brand': 'toyota', 'year': 2020, 'model': 'fortuner'}
        		     					    		
        		     					    	

How to Empty dictionary in python ?


in python we can empty ( remove all items ) the dictionary by calling the in-built clear() function on the dictionary object.
for example:
        		     					    		
modeldict = {
		"brand" : "suzuki",
		"year" : 2020,
		"model" : "ciaz",
		"fuel" : "petrol",
}

modeldict.clear()

print(modeldict)

output:

{}
        		     					    		
        		     					    	

How to get all the keys in the dictionary in python ?


in python we can get all the keys in the dictionary by calling the in-built keys function on the dictionary object.
when calling key() function on the dictionary it will returns the object .
the returning object contains the dictionary keys as a list in it.
for example:
        		     					    		
cardict = {
		"brand" : "suzuki",
		"year" : 2019,
		"model" : "swift dzire",
		"fuel" : "diesel",
}

dictkeys = cardict.keys()

print(dictkeys)

output:

dict_keys(['brand', 'year', 'model', 'fuel'])