Skip to main content

Python Dictionary

Dictionary is combination of key-value pairs. whenever insert a element of Dictionary also insert a value. order of this element are unordered list. but very useful to access and also can be modify its value by key. There are created by using following syntax.

dictionaryItem= {key1:value1,key2:value2,...,keyN:valueN}

In given syntax dictionaryItem is an variable. dictionary item are inserted within curly brackets ({}). and this are are group of every key and value element. colon (:) are used between key-value pair. and comma (,) are used to separate that each element. for example.

#defined a Dictionary
dictionaryItem={"name":"python","type":"programming","version":3}
print(dictionaryItem)
Output
{'version': 3, 'type': 'programming', 'name': 'python'}

In this program create a dictionary of three element. here (name, version, type) is keys and its value like that (python, 3, programming).

dictionary example

This is simplest example of dictionary item. so how to use this dictionary value. there is very simple, help of key that we can access that value. let view an example.

#defined a Dictionary
dictionaryItem={"name":"python","type":"programming","version":3}
print(dictionaryItem) # display all elements

#access element
print(dictionaryItem['name']) # display name
print(dictionaryItem['type']) # display type
print(dictionaryItem['version']) # display all element
Output
{'version': 3, 'type': 'programming', 'name': 'python'}
python
programming
3

key of this are unique. there cannot have two same key are not defined inside a dictionary. see a example

#defined a Dictionary
dictionaryItem={1.0:"python2",1.0:"python3",1:"C",2:"C++"}
print(dictionaryItem) # display all elements
Output
{1.0: 'C', 2: 'C++'}

In this example there are three similar keys(1.0,1.0,1). In this case it will use one key and last value.

Multi Value dictionary

Dictionary value can be combination of group of tuple, list, other dictionary, sets elements. for example.

#defined a Dictionary
dictionaryItem={"list":['l1','l2','l2','l3'],
	"sets":{"s1","s2","s3"},
	"tuples":("t1","t2","t3","t4","t5"),
	"dict" :{"k1":{"v1","v2"},
		 "k2":{"v3","v4"}}
				}
print(dictionaryItem) # display all elements

In this program there are dictionary is a combination of different group of element.

mixed dictionary example

first index of this dictionary is a list and there is 4 elements. second index is dict(dictionary) and there is 2 elements. Third index is tuples that are group of 5 element. last index is sets that is contain 3 element. similar way we can create any number of hierarchical structure.

How to access multi-value list element?. there are similar to multidimensional array.

#defined a Dictionary
dictionaryItem={"list":['l1','l2','l2','l3'],
				"sets":{"s1","s2","s3"},
				"tuples":("t1","t2","t3","t4","t5"),
				"dict" :{"k1":{"v1","v2"},
						"k2":{"v3","v4"}}
				}
#display element values				
print(dictionaryItem["list"]) # display list

print(dictionaryItem["sets"]) # display sets

print(dictionaryItem["tuples"]) # display tuples

print(dictionaryItem["dict"]) # display dict

print(dictionaryItem["list"][1]) # l2

print(dictionaryItem["tuples"][3]) # t4

print(dictionaryItem["dict"]['k1']) # {"v1","v2"}
Output
['l1', 'l2', 'l2', 'l3']
set(['s3', 's2', 's1'])
('t1', 't2', 't3', 't4', 't5')
{'k2': set(['v3', 'v4']), 'k1': set(['v1', 'v2'])}
l2
t4
set(['v1', 'v2'])

Modifying dictionary

In this section we learn about how to add and update dictionary elements.

Add new element : This operation are very easy to dictionary. let take one example.

#defined a Dictionary
dictionaryItem={"name":"python","source":"code"}
print("Before Add")
print(dictionaryItem)

#add new key-value pair
dictionaryItem['version']=2
print("After Add")
print(dictionaryItem)
Output
Before Add
{'source': 'code', 'name': 'python'}
After Add
{'source': 'code', 'version': 2, 'name': 'python'}

adding new pair value of dictionary need new key and some associative value. when using of variable name and its index so we can get value of dictionary. similar way when add new element in existing dictionary there are need new key and its value. so similarly concepts are of in given above program.

update element: update are very similar to add new element. but here index value are used, that is already exist in dictionary. for example

#defined a Dictionary
dictionaryItem={"name":"java","source":"code"}
print("Before update")
print(dictionaryItem)

#update value
dictionaryItem['name']="python"
print("After update")
print(dictionaryItem)
Output
Before update
{'source': 'code', 'name': 'java'}
After update
{'source': 'code', 'name': 'python'}

In this program are modified name index value. similar way we can are change any index value at any time.

delete item: del statement can used to remove any key-value pair in dictionary. for example.

#defined a Dictionary
dictionaryItem={"name":"c","source":"code"}
print("Before delete")
print(dictionaryItem)

#delete value
del dictionaryItem['name']
print("After delete")
print(dictionaryItem)
Output
Before delete
{'source': 'code', 'name': 'c'}
After delete
{'source': 'code'}




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment