0% found this document useful (0 votes)
2 views7 pages

Dictionary Python

The document provides an overview of dictionaries in programming, highlighting their mutable and unordered nature, and their structure as key:value pairs. It explains how to create, access, update, and delete elements in a dictionary, as well as methods for traversing and checking membership. Additionally, it includes code examples to illustrate these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Dictionary Python

The document provides an overview of dictionaries in programming, highlighting their mutable and unordered nature, and their structure as key:value pairs. It explains how to create, access, update, and delete elements in a dictionary, as well as methods for traversing and checking membership. Additionally, it includes code examples to illustrate these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dictionaries

Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs the
associate keys to value.

Creating a Dictionary
To create a dictionary follow this syntax:
<dictionary-object> = {<key>:<value>,<key:value>,.....}

d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh


Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}

d = {1:'Sharda',2:'Champa',3:'Babita',4:'Pushpa',5:'Chandirka',6:'Meena'}

d = {1:100,2:200,3:300,4:400}

Creating an empty dictionary


d = {}
As simple as that we have created an empty lists, empty tuple, you can also create an empty
dictionary and manipulate it afterwards.
Dictionaries are also known as associative arrays or mappings or hashes.

Accessing elements of the dictionary


Actually dictionaries are indexed based on their keys. So whenever you want to access the values of
it keys are used to access them. Observe the following:
d = {1:'Virat Kohli',2:'Ajinkya Rehane',3:'Subhman Gill'}

#Priting with keys


print(d[1],d[3])

#Printing all values


print(d)

The process of taking a key and finding a value from the


dictionary is known as lookup. Moreover, you cannot a access any
element without key. If you try to access a value with key doesn’t
exist in the dictionary, will raise an error.

Now have a look at the following code:


d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh
Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
for i in d:
print(i, ":",d[i])

In the above code you can see the variable i prints the key and d[i] prints the associated value to the
key.
d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh
Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
for i,j in [Link]():
print(i, ":",j)

Here, I have separated the values using two variables in the loop.
You can also access the keys and values using [Link]() and d. values() respectively. It will return
the keys, and values in the form of sequence. Observe this code and check the output yourself:
d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh
Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
print([Link]())
print([Link]())

TRAVERSING A DICTIONARY

We can access each item of the dictionary or traverse a dictionary using for
loop.

>>> dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}

Method 1
>>> for key in dict1:

print(key,':',dict1[key])

Mohan: 95

Ram: 89

Suhel: 92
Sangeeta: 85

Method 2
>>> for key,value in [Link]():
print(key,':',value)
Mohan: 95
Ram: 89
Suhel: 92
Sangeeta: 85

Add elements to a dictionary


You can add an element to the dictionary with the unique key which is not already exist in the
dictionary. Look at the following code:
pr = dict([['name','Ujjwal'],['age',32],['salary',25000],['city','Ahmedabad']])
pr['dept']='school'
print(pr)

Update an element in a dictionary


To update the element, use a specific key and assign the new value for the dictionary. Observe this
code:
d = dict({'name':'Ujjwal','age':32,'salary':25000,'city':'Ahmedabad'})
d['salary']=30000
print(d)

Deleting element from the dictionary


You can delete the elements by using del, pop() and popitem() function. The pop() function we
will discuss in another article. Observe the following code for del:
d = dict({'name':'Shyam','age':32,'salary':25000,'city':'Ahmedabad'})
del d['age']

Membership Operator
As you know in and not in operator we have used with list and tuple, similarly used with a
dictionary. If the key is present in the dictionary, it will return true otherwise false. Look at the
code:
d = dict({'name':'Shyam','age':32,'salary':25000,'city':'Ahmedabad'})
if 'name' in d:
print("Key found")
else:
print("Key not found")
if 'Shyam' in [Link]():
print("Value found")
else:
print("Value not found")

The process of taking a key and finding a value from the dictionary is known as lookup. Moreover,
you cannot a access any element without key. If you try to access a value with key doesn’t exist in
the dictionary, will raise an error.
Now have a look at the following code:
d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh
Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
for i in d:
print(i, ":",d[i])

In the above code you can see the variable i prints the key and d[i] prints the associated value to the
key.
d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh
Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
for i,j in [Link]():
print(i, ":",j)

Here, I have separated the values using two variables in the loop.
You can also access the keys and values using [Link]() and d. values() respectively. It will return
the keys, and values in the form of sequence. Observe this code and check the output yourself:
d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh
Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
print([Link]())
print([Link]())

You might also like