SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Python Dictionary
Dictionary
• It is an unordered collection of data values, used to store data values like a map.
• Dictionary holds key:value pair.
• Key value is provided in the dictionary to make it more optimized.
• Each key-value pair in a Dictionary is separated by a colon :,
• whereas each key is separated by a ‘comma’.
• Key – must be unique and immutable datatype.
• Value – can be repeated with any datatype..
Dictionary - Create
# Creating an empty Dictionary
Dict = {}
# Creating a Dictionary with Integer Keys
Dict = {1: 'Zooming', 2: 'For', 3: 'Zooming’}
# Creating a Dictionary with Mixed keys
Dict = {'Name': 'Zooming', 1: [1, 2, 3, 4]}
# Creating a Dictionary with dict() method
Dict = dict({1: 'Zooming', 2: 'For', 3:'Zooming’})
# Creating a Dictionary with each item as a Pair
Dict = dict([(1, 'Zooming'), (2, 'For')])
Dictionary – Adding element
# Creating an empty Dictionary
Dict = {}
# Adding elements one at a time
Dict[0] = 'Zooming'
Dict[2] = 'For'
Dict[3] = 1
# Adding set of values
# to a single Key
Dict['Value_set'] = 2, 3, 4
# Updating existing Key's Value
Dict[2] = 'Welcome’
# Adding Nested Key value to Dictionary
Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'Zooming’}}
Dictionary – Accessing element
# Creating a Dictionary
Dict = {1: 'Zooming', 'name': 'For', 3: 'Zooming'}
# accessing a element using key
print("Acessing a element using key:")
print(Dict['name'])
# accessing a element using key
print("Acessing a element using key:")
print(Dict[1])
# accessing a element using get()
# method
print("Acessing a element using get:")
print(Dict.get(3))
Dictionary – Removing element
Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks’,
'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'},
'B' : {1 : 'Geeks', 2 : 'Life'}}
print("Initial Dictionary: ")
print(Dict)
# Deleting a Key value
del Dict[6]
# Deleting a Key from Nested Dictionary
del Dict['A'][2]
# Deleting a Key using pop()
Dict.pop(5)
# Deleting entire Dictionary
Dict.clear()
METHODS DESCRIPTION
copy() They copy() method returns a shallow copy of the dictionary.
clear() The clear() method removes all items from the dictionary.
pop() Removes and returns an element from a dictionary having the given key.
popitem() Removes the arbitrary key-value pair from the dictionary and returns it as tuple.
get() It is a conventional method to access a value for a key.
dictionary_name.values() returns a list of all the values available in a given dictionary.
str() Produces a printable string representation of a dictionary.
update() Adds dictionary dict2’s key-values pairs to dict
setdefault() Set dict[key]=default if key is not already in dict
keys() Returns list of dictionary dict’s keys
items() Returns a list of dict’s (key, value) tuple pairs
has_key() Returns true if key in dictionary dict, false otherwise
fromkeys() Create a new dictionary with keys from seq and values set to value.
type() Returns the type of the passed variable.
cmp() Compares elements of both dict.

More Related Content

PPTX
Python dictionary
PPTX
Python list
PPTX
Python dictionary
PDF
Python Variable Types, List, Tuple, Dictionary
PDF
Python list
PPTX
Tuple in python
Python dictionary
Python list
Python dictionary
Python Variable Types, List, Tuple, Dictionary
Python list
Tuple in python

What's hot (20)

PDF
Python Programming - XI. String Manipulation and Regular Expressions
PPTX
List and Dictionary in python
PPTX
Unit 4 python -list methods
PDF
Python libraries
PDF
PPTX
Chapter 15 Lists
PPTX
Values and Data types in python
PPTX
Recursion
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
PPTX
List in Python
PDF
What is Python Lambda Function? Python Tutorial | Edureka
DOCX
Data Structures Using C Practical File
PDF
Strings in Python
PDF
Python programming : Strings
PPT
Python Dictionaries and Sets
PDF
Python Collections Tutorial | Edureka
PPTX
Regular expressions
PPTX
Variables in python
Python Programming - XI. String Manipulation and Regular Expressions
List and Dictionary in python
Unit 4 python -list methods
Python libraries
Chapter 15 Lists
Values and Data types in python
Recursion
Pyhton with Mysql to perform CRUD operations.pptx
List in Python
What is Python Lambda Function? Python Tutorial | Edureka
Data Structures Using C Practical File
Strings in Python
Python programming : Strings
Python Dictionaries and Sets
Python Collections Tutorial | Edureka
Regular expressions
Variables in python
Ad

Similar to Python Dictionary (20)

PPTX
python advanced data structure dictionary with examples python advanced data ...
PDF
Python dictionaries
PPTX
Meaning of Dictionary in python language
PDF
Programming in python Unit-1 Part-1
PPTX
Untitled dictionary in python program .pdf.pptx
PPTX
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
PPTX
Python chapter 2
PPTX
python chapter 1
PPTX
Farhana shaikh webinar_dictionaries
PPTX
ch 13 Dictionaries2.pptx
PPT
2 UNIT CH3 Dictionaries v1.ppt
PPTX
6Dictionaries and sets in pythonsss.pptx
PPTX
Dictionaries.pptx
PPTX
Python list tuple dictionary .pptx
PPTX
Dictionary.pptx
PPTX
Python Interview Questions | Python Interview Questions And Answers | Python ...
DOC
Revision Tour 1 and 2 complete.doc
PPTX
Python-Dictionaries.pptx easy way to learn dictionaries
PPTX
dictionary14 ppt FINAL.pptx
python advanced data structure dictionary with examples python advanced data ...
Python dictionaries
Meaning of Dictionary in python language
Programming in python Unit-1 Part-1
Untitled dictionary in python program .pdf.pptx
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
Python chapter 2
python chapter 1
Farhana shaikh webinar_dictionaries
ch 13 Dictionaries2.pptx
2 UNIT CH3 Dictionaries v1.ppt
6Dictionaries and sets in pythonsss.pptx
Dictionaries.pptx
Python list tuple dictionary .pptx
Dictionary.pptx
Python Interview Questions | Python Interview Questions And Answers | Python ...
Revision Tour 1 and 2 complete.doc
Python-Dictionaries.pptx easy way to learn dictionaries
dictionary14 ppt FINAL.pptx
Ad

More from Soba Arjun (20)

PDF
Java interview questions
PDF
Java modifiers
PDF
Java variable types
PDF
Java basic datatypes
PDF
Dbms interview questions
PDF
C interview questions
PDF
Technical interview questions
PDF
Php interview questions with answer
PDF
Computer Memory Types - Primary Memory - Secondary Memory
PDF
Birds sanctuaries
PDF
Important operating systems
PDF
Important branches of science
PDF
Important file extensions
PDF
Java Abstraction
PDF
Java Polymorphism
PDF
Java Overriding
PDF
Java Inner Classes
PDF
java Exception
PDF
Java Methods
PDF
java Inheritance
Java interview questions
Java modifiers
Java variable types
Java basic datatypes
Dbms interview questions
C interview questions
Technical interview questions
Php interview questions with answer
Computer Memory Types - Primary Memory - Secondary Memory
Birds sanctuaries
Important operating systems
Important branches of science
Important file extensions
Java Abstraction
Java Polymorphism
Java Overriding
Java Inner Classes
java Exception
Java Methods
java Inheritance

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
master seminar digital applications in india
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Institutional Correction lecture only . . .
PDF
Basic Mud Logging Guide for educational purpose
Pre independence Education in Inndia.pdf
Pharma ospi slides which help in ospi learning
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
O7-L3 Supply Chain Operations - ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
master seminar digital applications in india
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPH.pptx obstetrics and gynecology in nursing
Final Presentation General Medicine 03-08-2024.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Complications of Minimal Access Surgery at WLH
Institutional Correction lecture only . . .
Basic Mud Logging Guide for educational purpose

Python Dictionary

  • 2. Dictionary • It is an unordered collection of data values, used to store data values like a map. • Dictionary holds key:value pair. • Key value is provided in the dictionary to make it more optimized. • Each key-value pair in a Dictionary is separated by a colon :, • whereas each key is separated by a ‘comma’. • Key – must be unique and immutable datatype. • Value – can be repeated with any datatype..
  • 3. Dictionary - Create # Creating an empty Dictionary Dict = {} # Creating a Dictionary with Integer Keys Dict = {1: 'Zooming', 2: 'For', 3: 'Zooming’} # Creating a Dictionary with Mixed keys Dict = {'Name': 'Zooming', 1: [1, 2, 3, 4]} # Creating a Dictionary with dict() method Dict = dict({1: 'Zooming', 2: 'For', 3:'Zooming’}) # Creating a Dictionary with each item as a Pair Dict = dict([(1, 'Zooming'), (2, 'For')])
  • 4. Dictionary – Adding element # Creating an empty Dictionary Dict = {} # Adding elements one at a time Dict[0] = 'Zooming' Dict[2] = 'For' Dict[3] = 1 # Adding set of values # to a single Key Dict['Value_set'] = 2, 3, 4 # Updating existing Key's Value Dict[2] = 'Welcome’ # Adding Nested Key value to Dictionary Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'Zooming’}}
  • 5. Dictionary – Accessing element # Creating a Dictionary Dict = {1: 'Zooming', 'name': 'For', 3: 'Zooming'} # accessing a element using key print("Acessing a element using key:") print(Dict['name']) # accessing a element using key print("Acessing a element using key:") print(Dict[1]) # accessing a element using get() # method print("Acessing a element using get:") print(Dict.get(3))
  • 6. Dictionary – Removing element Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks’, 'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'}, 'B' : {1 : 'Geeks', 2 : 'Life'}} print("Initial Dictionary: ") print(Dict) # Deleting a Key value del Dict[6] # Deleting a Key from Nested Dictionary del Dict['A'][2] # Deleting a Key using pop() Dict.pop(5) # Deleting entire Dictionary Dict.clear()
  • 7. METHODS DESCRIPTION copy() They copy() method returns a shallow copy of the dictionary. clear() The clear() method removes all items from the dictionary. pop() Removes and returns an element from a dictionary having the given key. popitem() Removes the arbitrary key-value pair from the dictionary and returns it as tuple. get() It is a conventional method to access a value for a key. dictionary_name.values() returns a list of all the values available in a given dictionary. str() Produces a printable string representation of a dictionary. update() Adds dictionary dict2’s key-values pairs to dict setdefault() Set dict[key]=default if key is not already in dict keys() Returns list of dictionary dict’s keys items() Returns a list of dict’s (key, value) tuple pairs has_key() Returns true if key in dictionary dict, false otherwise fromkeys() Create a new dictionary with keys from seq and values set to value. type() Returns the type of the passed variable. cmp() Compares elements of both dict.