Dictionary<\/strong><\/p>\nAnother useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are unique within a dictionary. The main operation of a dictionary is storing a value corresponding to a given key and extracting the value for that given key. Unlike sequences, which are indexed by a range of numbers, the dictionary is indexed by key (key should be of an immutable type, strings and numbers can always be keys).<\/p>\n
Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. The list cannot be used as keys, since lists are of mutable type. Also, as mentioned previously, Python allows adding a trailing comma after the last item of the dictionary.<\/p>\n
>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a\r\n{ ' sape ' : 4139 , ' jack ' : 4098 , ' guido ' : 4127 }\r\n>>> a [ ' jack ' ]\r\n4098\r\n>>> a= { ' sape ' : 4139, ' guido ' : 4127, ' jack ' : 4098 , }\r\n>>> a\r\n{ ' sape ' : 4139 , ' jack ' : 4098 , ' guido ' : 4127 }<\/pre>\nDictionary creation<\/strong><\/p>\nDictionary can be created in many ways.<\/p>\n
Using curly braces<\/strong><\/p>\nPlacing a comma-separated record of key-value pairs within the braces adds initial key-value pairs to the dictionary; this is also the way dictionaries are written as output.<\/p>\n
>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a \r\n{ ' sape ' , : 4139 , ' jack ' : 4098 , ' guido ' : 412 7 }\r\n\r\nA pair of braces creates an empty dictionary.\r\n\r\n>>> a= { }\r\n>>> a \r\n{ }\r\n>>> type ( a ) \r\n<type ' dict ' ><\/pre>\nDictionary comprehension<\/strong><\/p>\nDictionary comprehension provides a concise way to create a dictionary.<\/p>\n
>>> { x : x**2 for x in ( 2 , 4 , 6 ) } \r\n{ 2 : 4 , 4 : 16 , 6 : 36 }<\/pre>\nUsing built-in function<\/strong><\/p>\nDictionary can also be created using a built-in function diet ( ). Consider the following example using diet (), which returns the same dictionary { ” one ” : 1 , ” two ” : 2 , ” three ” : 3 } :<\/p>\n
>>> a=dict ( one=1 , two=2 , three=3 )\r\n>>> b= { ' one ' : 1 , ' two ' : 2 , ' three ' : 3 }\r\n>>> c=dict ( zip ( [ ' one ' , ' two ' , ' three ' ] , [ 1 , 2 , 3 ] ) ) \r\n>>> d=dict ( [ ( ' two', 2), ( ' one ', 1), ( ' three ', 3) J )\r\n>>> e=dict ( { ' three ' : 3 , ' one ' : 1 , ' two ' : 2 } )\r\n>>> a==b==c==d==e \r\nTrue<\/pre>\nAccessing dictionary elements<\/strong><\/p>\nTo access a dictionary value, use the key enclosed within the square bracket. A KeyError exception is raised if the key is not present in the dictionary.<\/p>\n
>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a [ ' guido ' ]\r\n4127<\/pre>\nUpdating dictionary elements<\/strong><\/p>\nIt is possible to add a new item in a dictionary and can also change the value for a given key.<\/p>\n
>>> a= { 'sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a [ ' mike ' ]=2299 # Add new item\r\n>>> a [ ' guido ' ]=1000 # Update existing item\r\n>>> a\r\n{ ' sape ' : 4139 , ' mike ' : 2299 , ' jack ' : 4098 , ' guido ' : 1000 }<\/pre>\nIt is also possible to update a dictionary with another dictionary using the update ( ) method (discussed later).<\/p>\n
Deleting dictionary elements<\/strong><\/p>\nTo remove a dictionary item (key-value pair) or the entire dictionary, one can use the del statement. To remove all items (resulting in an empty dictionary), the clear ( ) method (discussed later) can be used.<\/p>\n
>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 } \r\n>>> del a [ ' guido ' ]\r\n>>> a\r\n{ ' sape ' : 4139 , ' jack ' : 4098 }\r\n>>> del a\r\n>>> a\r\nTraceback ( most recent call last ) :\r\nFile \" <stdin> \", line 1, in <module>\r\nNameError: name ' a ' is not defined<\/pre>\nMembership operation<\/strong><\/p>\nDictionary support membership operation i.e. checking the existence of a key in the dictionary.<\/p>\n
>>> a= { 'sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> ' jack ' in a \r\nTrue\r\n>>> ' tom ' not in a \r\nTrue\r\n>>> 4127 in a \r\nFalse<\/pre>\nLooping techniques<\/strong><\/p>\nWhen looping through the dictionary, the key and corresponding value can be retrieved at the same time using the iteritems ( ) method.<\/p>\n
>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> for k , v in a . iteritems ( ) :\r\n. . . print k , v\r\nsape 4139 \r\njack 4098 \r\nguido 4127<\/pre>\n","protected":false},"excerpt":{"rendered":"In this Page, We are Providing Python Programming \u2013 Dictionary. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Dictionary Another useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are …<\/p>\n
Python Programming \u2013 Dictionary<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Dictionary - Python Programs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n