[go: up one dir, main page]

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

Dictionary

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Dictionary

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DICTIONARY IN

PYTHON

INTRODUCTION:

 Dictionary is a collection of elements which is unordered, changeable and indexed.


 Dictionary has keys and values.
 Doesn’t have index for values. Keys work as indexes.
 Dictionary doesn’t have duplicate member means no duplicate key.
 Dictionaries are enclosed by curly braces { } separated by commas ( , )
 A dictionary key can be almost any Python type, but are usually numbers or strings.

 Values can be assigned and accessed using square brackets [ ].

CREATING A DICTIONARY:

Syntax:

dictionary-name = {key1:value, key2:value, key3:value, keyn:value}

Example:

>>> marks = { "physics" : 75, "Chemistry" : 78, "Maths" : 81, "CS":78 }

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 78}

ACCESSING ELEMENTS OF A DICTIONARY:

Syntax:

dictionary-name[key]

Example:

>>> marks = { "physics" : 75, "Chemistry" : 78, "Maths" : 81, "CS":78 }

>>> marks["Maths"]
81
>>> marks.keys( ) #To access all keys in one go

dict_keys(['physics', 'Chemistry', 'Maths', 'CS'])

>>> marks.values( ) # To access all values in one go

dict_values([75, 78, 81, 78])

Lookup : A dictionary operation that takes a key and finds the corresponding value, is

called lookup.

TRAVERSING A DICTIONARY:

Syntax:

for <variable-name> in <dictionary-name>:

statement

Example:

>>> for i in marks:

print(i, ": ", marks[i])


OUTPUT:

physics : 75

Chemistry : 78

Maths : 81

CS : 78

CHANGE AND ADD THE VALUE IN A DICTIONARY: Syntax:

dictionary-name[key]=value
Example:

>>> marks = { "physics" : 75, "Chemistry" : 78, "Maths" : 81, "CS":78 }

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 78}

>>> marks['CS']=84 #Changing a value

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 84}

>>> marks['English']=89 # Adding a value

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 84, 'English': 89}

DELETE ELEMENTS FROM A DICTIONARY:

There are two methods to delete elements from a dictionary:

(i) using del statement

(ii) using pop( ) method

(i) Using del statement:

Syntax:

del dictionary-name[key]

Example:

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 84, 'English': 89}

>>> del marks['English']

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 84}


(ii) Using pop( ) method: It deletes the key-value pair and returns the value of

deleted element.

Syntax:

dictionary-name.pop( )

Example:

>>> marks

{'physics': 75, 'Chemistry': 78, 'Maths': 81, 'CS': 84}

>>> marks.pop('Maths')

81

PRETTY PRINTING A DICTIONARY:

To print a dictionary in more readable and presentable form.

For pretty printing a dictionary you need to import json module and then you can use

dumps( ) function from json module.

Example:

>>> print(json.dumps(marks, indent=2))

OUTPUT:

"physics": 75,

"Chemistry": 78,

"Maths": 81,

"CS": 78

}
dumps( ) function prints key:value pair in separate lines with the number of spaces

which is the value of indent argument.

DICTIONARY FUNCTIONS:

You might also like