0% found this document useful (0 votes)
13 views

Dic in Python

Dictionary in python
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)
13 views

Dic in Python

Dictionary in python
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/ 6

Dictionary in Python

Points to Learn:
 Introduction of Dictionary
 Creation of Dictionary
 Accessing and Traversing a Dictionary
 Common Dictionary Operations:
1. Appending values to Dictionary
2. Updating Elements in a Dictionary
3. Membership Operators ‘in’ and ‘not in’
4. Removing an item from Dictionary
 Common dictionary function and methods
What is Dictionary:
A python dictionary is a mapping of unique keys to values. It is a collection of key-value pairs. Dictionaries
are mutable which means they can be changed.
Python dictionary is an unordered collection of items where each item is a key-value pair.
List=[10,40,20,50,60,20]
d1= { ‘Input’:’Keyboard’,’Output’:’printer’,’Language’:’python’,’OS’:’Windows’ }
Creating a Dictionary:
Empty dictionary:
d={}
d=dict()
We can store int, float, string, list or tuple in dictionary as value of key.
Adding an item in Dictionary:
D={}
D[‘input’]=’Keyboard’
D[‘Output’]=’Printer’
D[‘Language’]=’Python’

Accessing a Dictionary:
To access dictionary elements, you can use the square brackets along with the key to obtain its
value.

Traversing a Dictionary:
Traversing a dictionary means accessing each element of a Dictionary. This can be done by
using either for or while looping statement.

Common Dictionary operations:


 Appending values to Dictionary
 Updating Elements in a Dictionary
 Membership Operators ‘in’ and ‘not in’
 Removing an item from Dictionary

Dictionary Functions:
 len(): This function return the number of key-value pairs in the dictionary.
Syntax: len(dic_name)
 clear(): This function removes all items from the particular dictionary.
Syntax: d.clear()
 get(): this function returns a value for the given key. If key is not available, then returns
default value none.
Syntax: d.get(key,default=None)
 items(): This function returns the content of dictionary as a list of tuples having key-value
pairs.
Syntax: d.items()
 keys(): This function returns a list of the key values in a dictionary.
Syntax: d.keys()
 values(): This function returns a list of values from key-value pairs in a dictionary.
Syntax: d.values()
 fromkeys(): This function is used to create dictionary from a collection of keys.
Syntax: d.fromkeys(ck,v)
 copy(): This function is used to create copy of dictionary.
Syntax: d.copy()
 popitem(): This function is used to removes last items from dictionary and returns this
deleted item.
Syntax: d.popitem()
 setdefault(): This function is used to returns the value of the item with specified key. If the
key does not exist, it inserts the key with the specified value.
Syntax: d.setdefault(key,default_value)
 max() and min(): These functions returns maximum and minimum value of key.
Syntax: max(d)
 sorted(): This function is used to sorts the elements of a dictionary by its key or value.
Syntax: sorted(d)

You might also like