0% found this document useful (0 votes)
15 views17 pages

Revised Dictionary-4-39

The document provides a comprehensive overview of the dictionary data type in Python, detailing its definition, creation, and various functions and methods such as len(), dict(), keys(), values(), and more. It explains how dictionaries store unique key-value pairs, allows for modification of elements, and includes information on membership operators to test key existence. Additionally, it covers specific methods for accessing, updating, and deleting items within a dictionary.
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)
15 views17 pages

Revised Dictionary-4-39

The document provides a comprehensive overview of the dictionary data type in Python, detailing its definition, creation, and various functions and methods such as len(), dict(), keys(), values(), and more. It explains how dictionaries store unique key-value pairs, allows for modification of elements, and includes information on membership operators to test key existence. Additionally, it covers specific methods for accessing, updating, and deleting items within a dictionary.
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
You are on page 1/ 17

DICTIONARY

INTRODUCTION
DICTIONARY: Definition
CONTENTS Dictionary data type is an  Let, us imagine that there are
Introduction : unordered set of comma-separated some set of lockers (/Dictionary),
Definition, key: value pairs, within { } , with the Each locker, with a unique KEY
Creation, requirement that within a dictionary, and some set of things (value) in
Accessing elements, no two keys can be the same (i.e. it, is a Key:Value pair.
Add an item,
Modify an item, there are unique keys within a
Traversal, dictionary).
Membership operator We can change dictionary
Functions/Methods :  We can keep different things
elements. and change the items of the locker
len(), dict(), keys(),
values(), items(), get(), It can contain values of mixed data Locker can hold any thing viz.
update(), del(), del, clear(), types. dresses, books, files etc.
fromkeys(), copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted()
Value
copy();
Suggested programs D = { 10: 'CS' , 'IP': 50 }
Key
Modify an item in DICTIONARY
CONTENTS
Introduction :
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods :
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del, clear(),
fromkeys(), copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted()
copy();
Suggested programs :
Here, Changes/modifications reflected in the encircled key:value pairs
Traversal in a DICTIONARY
CONTENTS
Introduction :
Definition,
Creation,
Accessing elements,
Add an item, OUTPUT
Modify an item,
Traversal,
Membership operator
Functions/Methods :
len(), dict(), keys(), OUTPUT
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(), min(),
sorted() copy(); OUTPUT
Suggested programs :
MEMBERSHIP OPERATOR in a DICTIONARY
Membership operator IN, NOT IN tests for Existence of a Key in a
CONTENTS Dictionary. Returns Boolean True or False
Introduction : OPERATOR DESCRIPTION
Definition,
Creation, in Results to True , if it finds the KEY in the DICTIONARY,
Accessing elements, else it returns False
Add an item,
not in Results to True , if it DOES NOT find the KEY in the
Modify an item,
Traversal, DICTIONARY, else it returns False

Membership
operator
Functions/Methods –
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(), min(),
sorted();
Suggested programs
FUNCTIONS /
METHODS
DICTIONARY
FUNCTIONS IN DICTIONARY: len()
CONTENTS
Introduction :
Definition,
len( )
Creation, It returns length of the dictionary i.e. number of
Accessing elements,
Add an item, elements. Each element is a key:value pair.
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
Ex. One of the
setdefault(), max(), min(),
key:value pair
sorted() ;
Suggested programs :
FUNCTIONS IN DICTIONARY: dict()
Creating empty dictionary with
dict( ) dict() method
It is a built-in function that returns a dictionary object
or simply creates a dictionary in Python.
Creating Dictionary from Key and Value Pairs in the form of sequences
Creating Dictionary using aTUPLE OF LISTS Creating Dictionary using a LIST OF LISTS containing
containing [key,value] pair using dict() method [key,value] pair using dict() method

Creating Dictionary using a TUPLE OF TUPLES


Creating Dictionary using a LIST OF TUPLES
containing (key,value) pair using dict()
containing (key,value) pair using dict() method
method

Creating Dictionary by zipping two Lists /or two Tuples containing Key and Values using zip( ) method and
sending the zipped sequences of key and values to the dict() method as parameter.
FUNCTIONS IN DICTIONARY: keys()
CONTENTS keys( )
Introduction : Returns a list containing the dictionary's keys
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal, Membership operator
Functions/Methods:
keys()
len(), dict(), ,
values(), items(), get(), update(),
del(), del, clear(), fromkeys(),
copy(), pop(), popitem(),
setdefault(), max(), min(),
sorted() ;
Suggested programs
LIST of all the keys of
EXTRA FACTS the key:value pairs
If a Dictionary is created with similar keys, then the
key is considered only once and its Value is from the
last occuring value of the key:value pair. Here, the key
10 has values ‘x’ and then ‘z’. So, the last occuring
value ‘z’ is taken as its value.
FUNCTIONS IN DICTIONARY: values()
CONTENTS values( )
Introduction : Returns a list of all the values in the dictionary
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(),
get(), update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(), min(),
sorted() ;
Suggested programs : LIST of all the values of the key:value pair
FUNCTIONS IN DICTIONARY: items()
CONTENTS
Introduction :
items( )
Definition, Returns a list containing a tuple for each key
Creation, value pair.
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(),items() ,
get(), update(), del(),
del, clear(), fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted() ;
Suggested programs
FUNCTIONS IN DICTIONARY: get()
CONTENTS
Introduction :
get( )
Definition, Returns the value of the specified key
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(),

get(), update(),
del(), del, clear(),
fromkeys(), copy(),
pop(), popitem(), Elements can be accessed using get() method with
setdefault(), max(),
min(), sorted() ; Dictionary or directly by writing the key inside [ ]
Suggested programs : with the Dictionary name
FUNCTIONS IN DICTIONARY: update()
CONTENTS update( )
Introduction : Updates the dictionary with the specified key-value pairs.
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(), get(),

update(),
del(), del, clear(),
fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(),
min(), sorted() ;
Suggested programs
FUNCTIONS IN DICTIONARY: del()
CONTENTS
Introduction :
del( ) method
Definition, It removes an entire dictionary
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),

update(), del() , del,


clear(), fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(),sorted();
Suggested programs
KEYWORD IN DICTIONARY: del
CONTENTS del keyword
• It removes a key-value pair from the dictionary based on the key.
Introduction : • If the key is present in the dictionary then it will delete the key and
Definition, corresponding value from the dictionary.
Creation, • But if the given key is not present in the dictionary then it will
Accessing elements, throw an error i.e. KeyError.
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),

update(), del(), del ,


clear(), fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(),sorted() ;
Suggested programs
FUNCTIONS IN DICTIONARY: clear()
CONTENTS
Introduction :
Definition, clear( )
Creation, Removes all the elements from the dictionary.
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods –
len(), dict(), keys(), values(),
items(), get(), update(), del(),

del,clear() ,
fromkeys(), copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted() ;
Suggested programs :
FUNCTIONS IN DICTIONARY: fromkeys()
CONTENTS fromkeys( )
Introduction : Returns a dictionary with the specified keys and value.
Definition, Syntax
Creation, dict.fromkeys(keys, value)
Accessing elements,
Add an item,
Parameter Values
Modify an item, Parameter Description
Traversal, keys Required. An iterable specifying the keys of the new dictionary
Membership operator value Optional. The value for all keys. Default value is None
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(),
fromkeys(),
copy(), pop(), popitem(),
setdefault(), max(),
min(), sorted();
Suggested programs

You might also like