Python dictionary update - comprehension

Python Dictionary Update - Comprehension

                        

this section we learn about python dictionary update and python dictionary comprehension.

 




python dictionary is use to store data as key and value form. it is also knows as collection of word with is meaning. It is most use in python program as 






Library Management System and word dictionary. Example of key and value




 

dictionary_name = {"key":"value"}

It is the example of dictionary. How to update our dictionary in python. Dictionary update is use to join two dictionary in one dictionary or upade our old dictionary to new. We must use dictname .update(key,value). It is pass tuple value to our dictionary. 

mydict = {"samir":"it is a good boy",
"hari":"it is bab boy",
"mark":[1,2,3,4],
"enterdic":{"ok":"its means ok",
"short":"it meams bad"},
"last":"it is last"
}
print(mydict["last"])

It is a simple example of Dictionary. dictionary name is mydict. mydict key are samir,hari, mark, enterdic is dictionary of dictionary ,ok, short, last and the value it is good boy , it is bad boy , 1234 ,its means ok, it mens, it is last. Last print mydict of last key then print of last key of value.

OUTPUT:


it is last

Python Dictionary Update Method

Here is now some Dictionary fuction like items value key __len__ etc

mydict = {"samir":"it is a good boy",
"hari":"it is bab boy",
"mark":[1,2,3,4],
"enterdic":{"ok":"its means ok",
"short":"it meams bad"},
"last":"it is last",
1:2

}
mydict["hari"] = "it is a good"

print(mydict["hari"])
print(mydict.keys())
print(list(mydict))
print(mydict.__len__())
#print(mydict.values())
print(mydict.items())
updatedic = {"dahal":"yes it is a casst",
"hari":"it is a dancer"
}
print(mydict.get("hari"))
mydict.update(updatedic)
print(mydict)

mydict have given key and value. Ther mydict hari key is update the value in it is a good.__len__ fuction use to count the dictionary key. Dictionary shows all key and value of a mydict. Udatedic is a another dictionary with key and value dahal and hari. Mydict update with .get and udate method. See in out all answer what happen   

it is a good
dict_keys(['samir', 'hari', 'mark', 'enterdic', 'last', 1])
['samir', 'hari', 'mark', 'enterdic', 'last', 1]
6
dict_items([('samir', 'it is a good boy'), ('hari', 'it is a good'), ('mark', [1, 2, 3, 4]), ('enterdic', {'ok': 'its means ok', 'short': 'it meams bad'}), ('last', 'it is last'), (1, 2)])
it is a good
{'samir': 'it is a good boy', 'hari': 'it is a dancer', 'mark': [1, 2, 3, 4], 'enterdic': {'ok': 'its means ok', 'short': 'it meams bad'}, 'last': 'it is last', 1: 2, 'dahal': 'yes it is a casst'}

get update and .update is dictionary update with overwrite value and not overwrite.

Python Dictionary Project

Make a project like word meaning like Mother language and English Language such user input kitab as Mother Language than your program is capable to print kitab as english language Book kitab=book. 
#practice set
neplaidict = {
    "pakha":"fan",
    "ketab":"book",
    "pana":"paper",
}
print("all options to search",neplaidict.keys())
a = input("enter your nepali word")

print("no optionn here",neplaidict.get(a))

It is a Example of Dictionary project. It is only hint of Dictionary Project. if you know about the Error handaling than use to error handaling. it is save your program from crash.

OUTPUT:

all options to search dict_keys(['pakha', 'ketab', 'pana'])
enter your nepali word: pana
pana optionn here paper

Python Dictionary Comprehension

Comprehension makes our program one line but it is not makes our program readable. That way begginner should be confuse in comprehension. Any program that use comprehension than hard to read that program.


codebugfree_dictionary = {} #it is a empty dictionary
for i in range(0,10):#range 0,9 
    codebugfree_dictionary[i] = i*2  
print(codebugfree_dictionary)

It is not a comprehension so it is easy to read and write this program.

OUTPUT:

 

{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}

Look the dictionary comprehension is how to make difficult a program.


codebugfree_dictionary = {} #it is a empty dictionary
for i in range(0,10):#range 0,9 
    codebugfree_dictionary[i] = i*2  
print(codebugfree_dictionary)

codebugfree_dictionary2 = {i:i*2 for i in range(0,10)}
print(codebugfree_dictionary2

Codebugfree_dictionary2 is a Dictionary comprehension. Compare with codebugfree_dictionary and codebugfree_dictionary2. Second one is oneline but it is hard to read. Two code come same output

OUTPUT:

{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}


Post a Comment

0 Comments