{"id":1440,"date":"2022-03-27T19:40:18","date_gmt":"2022-03-27T19:40:18","guid":{"rendered":"https:\/\/visitgis.com\/?p=1440"},"modified":"2022-03-27T19:46:26","modified_gmt":"2022-03-27T19:46:26","slug":"python-dictionary","status":"publish","type":"post","link":"https:\/\/visitgis.com\/python-dictionary\/","title":{"rendered":"Python Dictionary"},"content":{"rendered":"\n
A python<\/a> dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are the keys that are used to access values within<\/a> a dictionary.<\/p>\n\n\n\n It is very simple to create a dictionary as placing items inside {} separated by , commas.<\/p>\n\n\n\n Each item will have a KEY and a corresponding VALUE consecutively, it will look like {Key:Value}.<\/strong> <\/p>\n\n\n\n While the values can be of any data type and can repeat, keys must be of immutable type and must be unique.<\/p>\n\n\n\n In [59]:<\/p>\n\n\n\n Out[59]:<\/p>\n\n\n\n In [76]:<\/p>\n\n\n\n Out[76]:<\/p>\n\n\n\n Dict [(0,1)]<\/p>\n\n\n\n In [86]:<\/p>\n\n\n\n Out[86]:<\/p>\n\n\n\n In [87]:<\/p>\n\n\n\n Out[87]:<\/p>\n\n\n\n In [88]:<\/p>\n\n\n\n Out[88]:<\/p>\n\n\n\n In [89]:<\/p>\n\n\n\n Out[89]:<\/p>\n\n\n\n In [91]:<\/p>\n\n\n\n Out[91]:<\/p>\n\n\n\n In [92]:<\/p>\n\n\n\n Out[92]:<\/p>\n\n\n\n In [93]:<\/p>\n\n\n\n Out[93]:<\/p>\n\n\n\n In [94]:<\/p>\n\n\n\n Out[94]:<\/p>\n\n\n\n In [13]:<\/p>\n\n\n\n Out[13]:<\/p>\n\n\n\n In [19]:<\/p>\n\n\n\n Out[19]:<\/p>\n\n\n\n In [20]:<\/p>\n\n\n\n Out[20]:<\/p>\n\n\n\n In [21]:<\/p>\n\n\n\n Out[21]:<\/p>\n\n\n\n Methods that are available with a dictionary are tabulated below. Some of them have already been used in the above examples.<\/p>\n\n\n\n What is Python Dictionary? A python dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are the keys that are used to access values within a dictionary. It is very simple to create a […]<\/p>\n","protected":false},"author":1,"featured_media":1485,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","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":"","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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"categories":[129,58],"tags":[116,120,133,130,131,134,135,132],"class_list":["post-1440","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-gis-tutorials","tag-python","tag-python-dictionary","tag-python-dictionary-add-item","tag-python-dictionary-append","tag-python-dictionary-keys","tag-python-dictionary-methods","tag-python-dictionary-pop","tag-python-dictionary-update"],"yoast_head":"\n<\/figure>\n\n\n\n
# This is example of Dictonary <\/em>\nDict =<\/strong> {\"A\" : 1, \"B\" : 2, \"Key3\" : 3, \"Key4\" : 4}\nDict\n<\/pre>\n\n\n\n
{'A': 1, 'B': 2, 'Key3': 3, 'Key4': 4}<\/pre>\n\n\n\n
#Access Dictionary Values<\/em>\nDict [\"A\"]\n<\/pre>\n\n\n\n
1<\/pre>\n\n\n\n
Each key is separated from its value by a colon “:”. Commas separate the items, and the whole dictionary is enclosed in curly braces. An empty python dictionary without any items is written with just two curly braces, like this “{}”.<\/h5>\n\n\n\n
# Create a sample dictionary<\/em>\n\nName_Age_Dict =<\/strong> {\"Umair\": \"1985\", \"Simmi\": \"1980\", \\\n \"Moon\": \"1973\", \"Sumaira\": \"1992\", \\\n \"Ali\": \"1977\", \"Asghar\": \"1976\", \\\n \"Tahira\": \"1977\", \"Fatima\": \"1977\"}\nName_Age_Dict\n#capital letter effects the results.<\/em>\n<\/pre>\n\n\n\n
{'Umair': '1985',\n 'Simmi': '1980',\n 'Moon': '1973',\n 'Sumaira': '1992',\n 'Ali': '1977',\n 'Asghar': '1976',\n 'Tahira': '1977',\n 'Fatima': '1977'}<\/pre>\n\n\n\n
# Get value by key<\/em>\n\nName_Age_Dict['Tahira'] \n<\/pre>\n\n\n\n
'1977'<\/pre>\n\n\n\n
# Get all the keys in dictionary<\/em>\n\nName_Age_Dict.<\/strong>keys() \n<\/pre>\n\n\n\n
dict_keys(['Umair', 'Simmi', 'Moon', 'Sumaira', 'Ali', 'Asghar', 'Tahira', 'Fatima'])<\/pre>\n\n\n\n
# Get all the values in dictionary<\/em>\n\nName_Age_Dict.<\/strong>values() \n<\/pre>\n\n\n\n
dict_values(['1985', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])<\/pre>\n\n\n\n
# Append value with key into dictionary<\/em>\n\nName_Age_Dict ['Tahira'] =<\/strong> '1992'\nName_Age_Dict\n<\/pre>\n\n\n\n
{'Umair': '1985',\n 'Simmi': '1980',\n 'Moon': '1973',\n 'Sumaira': '1992',\n 'Ali': '1977',\n 'Asghar': '1976',\n 'Tahira': '1992',\n 'Fatima': '1977',\n 'Graduation': '2007'}<\/pre>\n\n\n\n
# Delete entries by key<\/em>\n\ndel<\/strong>(Name_Age_Dict['Ali'])\nName_Age_Dict\n<\/pre>\n\n\n\n
{'Umair': '1985',\n 'Simmi': '1980',\n 'Moon': '1973',\n 'Sumaira': '1992',\n 'Asghar': '1976',\n 'Tahira': '1992',\n 'Fatima': '1977',\n 'Graduation': '2007'}<\/pre>\n\n\n\n
#verify<\/em>\n'Ali' in<\/strong> Name_Age_Dict\n<\/pre>\n\n\n\n
False<\/pre>\n\n\n\n
#verify<\/em>\n'Umair' in<\/strong> Name_Age_Dict\n<\/pre>\n\n\n\n
True<\/pre>\n\n\n\n
V=<\/strong>{'A','B'}\nV.<\/strong>add('C')\nV\n<\/pre>\n\n\n\n
{'A', 'B', 'C'}<\/pre>\n\n\n\n
A =<\/strong> {'1','2'} \na=<\/strong>set(A)\na\n<\/pre>\n\n\n\n
{'1', '2'}<\/pre>\n\n\n\n
type(set([1,2,3]))\n<\/pre>\n\n\n\n
set<\/pre>\n\n\n\n
{'a','b'} &<\/strong>{'a'}\n<\/pre>\n\n\n\n
{'a'}<\/pre>\n\n\n\n
Python Dictionary Methods<\/h2>\n\n\n\n
Method<\/th> Description<\/th><\/tr> clear()<\/a><\/td> Removes all items from the dictionary.<\/td><\/tr> copy()<\/a><\/td> Returns a shallow copy of the dictionary.<\/td><\/tr> fromkeys(seq[, v])<\/a><\/td> Returns a new dictionary with keys from seq<\/var> and value equal to v<\/var> (defaults to None<\/code>).<\/td><\/tr>
get(key[,d])<\/a><\/td> Returns the value of the key<\/var>. If the key<\/var> does not exist, returns d<\/var> (defaults to None<\/code>).<\/td><\/tr>
items()<\/a><\/td> Return a new object of the dictionary’s items in (key, value) format.<\/td><\/tr> keys()<\/a><\/td> Returns a new object of the dictionary’s keys.<\/td><\/tr> pop(key[,d])<\/a><\/td> Removes the item with the key<\/var> and returns its value or d<\/var> if key<\/var> is not found. If d<\/var> is not provided and the key<\/var> is not found, it raises KeyError<\/code>.<\/td><\/tr>
popitem()<\/a><\/td> Removes and returns an arbitrary item (key, value<\/strong>). Raises KeyError<\/code> if the dictionary is empty.<\/td><\/tr>
setdefault(key[,d])<\/a><\/td> Returns the corresponding value if the key<\/var> is in the dictionary. If not, inserts the key<\/var> with a value of d<\/var> and returns d<\/var> (defaults to None<\/code>).<\/td><\/tr>
update([other])<\/a><\/td> Updates the dictionary with the key\/value pairs from other<\/var>, overwriting existing keys.<\/td><\/tr> values()<\/a><\/td> Returns a new object of the dictionary’s values<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"