Professional Documents
Culture Documents
11-Python Dictionaries
11-Python Dictionaries
Dictionaries in Python
• Introduction
• Key:Value Pairs
• Practice Questions
UNIT RDBMS:
Data
manupulation
using SQL
Dictionary is a data type in Python which stores the data in
key value pairs.
Dictionary is a data type in Python which stores the data in
key value pairs.
dict1 = {}
Creating a Dictionary
• A dictionary can be created by placing key-value pairs
inside curly braces separated by comma.
dict1 = {}
D = dict()
Creating a Dictionary
Way – I
dict2 = {
'R': 'Red',
'B': 'Blue',
'G': 'Green'
}
Creating a Dictionary
Way – II
D={}
D['B'] = 'Blue'
D['R'] = 'Red'
D['G'] = 'Green
Creating a Dictionary
Way – II
{
D['B'] = 'Blue' 'B': 'Blue',
'R': 'Red',
D['R'] = 'Red' 'G': 'Green'
}
D['G'] = 'Green
Creating a Dictionary
Syntax
dictionaryName = {
key 1: value 1,
key 2: value 2,
.....}
dic1 = {
“Color”: “Blue”,
“Fruit”: “Mango”,
“Price”: 100
}
Creating a Dictionary
Syntax
Dictionaries are also called
dictionaryName = { "associative arrays" or "
mapping" or "hashes".
key 1: value 1,
key 2: value 2, A dictionary operation
.....} that takes a key and finds
the corresponding value is
called "lookup"
dic1 = {
“Color”: “Blue”,
“Fruit”: “Mango”,
“Price”: 100
}
Accessing a Dictionary
• In lists and strings, indexing is used to access the elements.
dictionaryName [Key]
Traversing a Dictionary
• Traversing is a process to iterate or access each element of a
dictionary.
dictionaryName [Key]
Accessing keys & values simultaneously
Syntax :
len (dictionaryName)
Functions/ Methods
Syntax :
dictionaryName.clear ( )
Functions/ Methods
Syntax :
dictionaryName.get (key)
Functions/ Methods
Syntax :
dictionaryName.get (key)
Functions/ Methods
Syntax :
dictionary1.update (dictionary2)
Thank you!