{"id":5049,"date":"2019-09-19T09:58:07","date_gmt":"2019-09-19T04:28:07","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5049"},"modified":"2019-09-23T10:41:56","modified_gmt":"2019-09-23T05:11:56","slug":"python-dictionary-operations","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/python-dictionary-operations\/","title":{"rendered":"Python Dictionary Operations"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Python Dictionary Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Date: 19\/09\/2019<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, I going to explain the basics of python dictionary operations and methods. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">INTRODUCTION<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python dictionary is an unordered collection of items and also it contains key-value pairs and enclosed by curly-braces { }.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Each key-value pair in a python dictionary is separated by a colon(:)  and each key is separated by a \u2018comma\u2019.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most importantly python dictionary, keys should not be repeated and values can be any datatype.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Create an empty dictionary: <\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">        First of all we can create the dictionary using the variable name for that we can use empty curly-braces. It will create an empty dictionary in python.<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img fetchpriority=\"high\" decoding=\"async\" width=\"575\" height=\"149\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_p1.png\" alt=\"\" data-id=\"5052\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5052\" class=\"wp-image-5052\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_p1.png 575w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_p1-300x78.png 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Create a dictionary with the<\/strong> <strong>element<\/strong>:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">         we can add items inside curly braces {} and separated by a comma for creating a dictionary with element.<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img decoding=\"async\" width=\"583\" height=\"214\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_p2.png\" alt=\"\" data-id=\"5053\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5053\" class=\"wp-image-5053\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_p2.png 583w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_p2-300x110.png 300w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Access elements from the dictionary<\/strong>:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We can access the dictionary elements using the key.<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img decoding=\"async\" width=\"577\" height=\"173\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_3.png\" alt=\"\" data-id=\"5054\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5054\" class=\"wp-image-5054\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_3.png 577w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/emptydict_3-300x90.png 300w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Update dictionary value:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We can add new items into the dictionary and also we can update the existing item.<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-4 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"187\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict4.png\" alt=\"\" data-id=\"5055\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5055\" class=\"wp-image-5055\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict4.png 581w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict4-300x97.png 300w\" sizes=\"(max-width: 581px) 100vw, 581px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Add element into the dictionary:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We can add the new element into the existing dictionary with the new key-value pair.<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-5 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"183\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict5.png\" alt=\"\" data-id=\"5056\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5056\" class=\"wp-image-5056\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict5.png 572w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict5-300x96.png 300w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Remove element from the dictionary:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We can remove the element from the dictionary using the pop() method, for that, we can give key name within the pop method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-6 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"571\" height=\"193\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict6.png\" alt=\"\" data-id=\"5057\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5057\" class=\"wp-image-5057\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict6.png 571w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict6-300x101.png 300w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Remove all item from the dictionary:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We can use clear() method to remove all element from the dictionary. It will display the empty dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-7 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"181\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict7.png\" alt=\"\" data-id=\"5058\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5058\" class=\"wp-image-5058\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict7.png 572w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict7-300x95.png 300w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Delete dictionary<\/strong>:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We can use the <strong>del<\/strong> method to delete the full dictionary.<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-8 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"142\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict8.png\" alt=\"\" data-id=\"5059\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=5059\" class=\"wp-image-5059\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict8.png 578w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/09\/dict8-300x74.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"> Thanks for using&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/\">Pheonix solutions<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> You find this tutorial helpful? Share with your friends.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Dictionary Operations Date: 19\/09\/2019 In this tutorial, I going to explain the basics of python dictionary operations and methods. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[713,714,292],"class_list":["post-5049","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-dictionary","tag-dictionary-operations","tag-python","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1jr","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5049"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5049\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}