0% found this document useful (0 votes)
36 views32 pages

Introduction To Python Programming (23ECAE21) Dictionary: SJB Institute of Technology

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

Introduction To Python Programming (23ECAE21) Dictionary: SJB Institute of Technology

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

║JAI SRI GURUDEV║

Sri Adichunchanagiri Shikshana Trust (R)

SJB INSTITUTE OF TECHNOLOGY


BGS Health & Education City, Kengeri , Bengaluru – 60

INTRODUCTION TO
PYTHON PROGRAMMING
[23ECAE21]
By:
DICTIONARY Surabhi B(1JB23EC108)
Sushith S (1JB23EC109)
Swati Rani (1JB23EC110)
TR Koushik (1JB23EC111)
Tanisha S (1JB23EC112)
Introduction To Dictionary
Definition Of Dictionary:
• Python dictionary is a data structure that stores the
value in ‘key’: value format.
This makes it different from lists, tuples, and
arrays as in a dictionary each key has an
associated value.

• Dictionaries are written with curly brackets ({})


Dictionary Syntax:
dict_var = {key1 : value1, key2 : value2, …..}

Example:
Dict = {1: 'Tic', 2: 'For', 3: 'Tac'}
print(Dict)

Output:
{1: 'Tic', 2: 'For', 3: 'Tac'}
Keys
• Keys are analogous to indexes of a list.
When using lists you access the elements via the
index. With dictionaries you access values via the
keys.

• A dictionary may contain duplicate values inside


it, but the keys MUST be unique so, it isn't
possible to access different values via the same
key.
Advantages of dictionary
• Uniqueness:
Keys in a dictionary must be unique.
• Fast Lookup:
Retrieving a value by its key in a dictionary is very fast, even for large
dictionaries.
• Flexibility:
Keys can be of various types, including strings, integers, tuples, etc.
• Key-Value Pairing:
The association between keys and values makes it easy to organize and
manipulate data in a structured manner.
• Dynamic:
Dictionaries are mutable, meaning you can add, remove, or modify key-value
pairs dynamically.
Disadvantages of dictionary
• Unordered:
Keys in dictionaries are unordered, meaning they don't maintain any particular
order.
• Memory Consumption:
Storing keys consumes memory.
• Immutable Requirement:
Dictionary keys must be immutable, which means you can't use mutable
objects like lists as keys.
• Key Collision:
Hash functions may produce the same hash value for different keys, leading to
collisions.
• Memory Overhead:
Dictionaries consume more memory compared to other data structures due to
their flexible nature.
Creating a dictionary

How to create a dictionary?


• In Python, a dictionary can be created by placing
a sequence of elements within curly {} braces,
separated by a ‘comma’.
SYNTAX:
Example:
Accessing an element
• To access a value in a dictionary, you use the key
associated with that value. Each key in a
dictionary is unique.
SYNTAX:
Example:
Adding/changing an element
• Once a set is created, you cannot change its items,
but you can add new items.
• To add one item in a set, use the add() method.

Example:
• To change the value of items within a specific range,
define a list with the new values, and refer to the
range of index numbers where you want to insert the
new values.
Example:
What are methods?
• methods are functions that are associated with an
object and can manipulate its data or perform
actions on it.
• Methods of dictionary inpython programming
are:-
Clear() copy() fromkeys() get()
Items() keys() pop() popitem()
Setdefault() update() values()
clear()
• Remove all the items from the dictonary
my_dict ={‘x’ : 1, ‘ y’ : 2}
my_dict.clear()
print(“Dict cleared:”,my_dict)

Output: Dict cleared: {}


copy()
• Returns a shallow copy of the dictionary
my_dict={ ‘x’ : 1, ’y’ : 2}
new_dict= my_dict.copy()
print(“Dict copied:”,new_dict)

Output: Dict copied:{‘x’ : 1, ‘y’ : 2}


get()
• Returns the value for specified key. If the key is not
found, returns the default value (default , None)
my_dict = {‘x’ : 1, ‘y’ : 2 }
value = my_dict.get(‘d’, ‘not found’)
print(“get value:”,value)

Output: get value: not found


items()
• Return a view object that displays a list of key-value
pairs as tuple
my_dict = { ‘x’ : 1, ‘y ’: 2 }
itmes = my_dict.items()
print(“Items:”,items)

Output :Items :dict_items([ (‘x’ ,1), (‘y’ ,2) ])


Keys()
• Returns a view object that displays a list of all keys in
the dictionary.
• The view object will reflect any changes done to the
dictionary.
• The keys of a dictionary are stored in a list format.
SYNTAX:
dict.keys()
pop()
• The pop method removes and returns an element from a
dictionary having given key.
• If mentioned key is found, key and value is removed
from the dictionary.
• If mentioned key is not found and default is mentioned,
the default argument would be passed.
• If mentioned key is not found and default is not
mentioned, a key error exception is raised.

SYNTAX:
dict.pop(key,[default])
Course code:23MET14D/24D Krishna Murthy N K /ASSISTANT PROFESSOR/MECHANICAL/SJBIT
pop()
• Condition-1:
If mentioned key is found, key and value is removed
from the dictionary.
pop()
• Condition-2:
If mentioned key is not found and default is mentioned,
the default argument would be passed.
pop()
• Condition-3:
If mentioned key is not found and default is not
mentioned, a key error is raised.

Course code:23MET14D/24D Krishna Murthy N K /ASSISTANT PROFESSOR/MECHANICAL/SJBIT


popitem()
• Popitem method removes the key and value in last in
first out order.
• An error is raised if the whole dictionary is empty.
SYNTAX:
dict.popitem()

Course code:23MET14D/24D Krishna Murthy N K /ASSISTANT PROFESSOR/MECHANICAL/SJBIT


setdefault()
• Returns the value of specified key.

• Returns the key value if key is in the dictionary.


• Returns NONE if key is not in the dictionary and default
value is not specified.
• Returns default value if key is not mentioned and default
value is mentioned.
SYNTAX:
dict.setdefault(key,[default_value])
setdefault()
• Condition-1:
Returns the key value if key is in the dictionary.
setdefault()
• Condition-2:
Returns NONE if key is not in the dictionary and default value is
not specified.
• Condition-3:
Returns default value if key is not mentioned and default value is
mentioned.
Update()
• Update() method updates a dictionary with elements of
another dictionary or a separate key value pair.
• The element is updated or appended at the last of the
dictionary.

SYNTAX:
dict.update()
values()
• This method returns all the values of all the keys without
displaying the keys.
• The output is shown in the list format.

SYNTAX:
dict.values()
Nested dictionary
• It is a collection of key-value pairs.
• In python, a dictionary is nested dictionaries are
dictionaries within dictionaries ,allowing for
complex data structures
Example:
nested_dict = { 'key1': {'nested_key1':
'value1', 'nested_key2': 'value2’},
'key2': {'nested_key3':
'value3', 'nested_key4': 'value4’} }
Difference between list ,dictionary and tuple :
Thank You

You might also like