Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

In today's session:

Dictionaries in Python

• Introduction

• Key:Value Pairs

• Working with Dictionaries

• Dictionary Functions and Methods

• 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.

1. Each key of dictionary maps to a value


Dictionary is a data type in Python which stores the data in
key value pairs.

1. Each key of dictionary maps to a value

2. Keys are unique while values may be not


Dictionary is a data type in Python which stores the data in
key value pairs.

1. Each key of dictionary maps to a value

2. Keys are unique while values may be not

3. Values can be of any type.


Creating a Dictionary
• A dictionary can be created by placing key-value pairs
inside curly braces separated by comma.

• Each key is separated from its value by a colon (:)


Creating a Dictionary
• A dictionary can be created by placing key-value pairs
inside curly braces separated by comma.

• Each key is separated from its value by a colon (:)

• To create an empty dictionary

dict1 = {}
Creating a Dictionary
• A dictionary can be created by placing key-value pairs
inside curly braces separated by comma.

• Each key is separated from its value by a colon (:)

• To create an empty dictionary

dict1 = {}

• Function dict() can be used to create a new dictionary with


not items.

D = dict()
Creating a Dictionary

Way – I

• Enter key value pairs directly in curly braces

dict2 = {
'R': 'Red',

'B': 'Blue',

'G': 'Green'
}
Creating a Dictionary

Way – II

• To initialize dictionary, values square brackets can be used as


follows:

D={}

D['B'] = 'Blue'

D['R'] = 'Red'

D['G'] = 'Green
Creating a Dictionary

Way – II

• To initialize dictionary, values square brackets can be used as


follows:

D={} print (D)

{
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.

• While in dictionary, key is used to access the elements

dictionaryName [Key]
Traversing a Dictionary
• Traversing is a process to iterate or access each element of a
dictionary.

• for loop is used to iterate all the elements of a dictionary.

dictionaryName [Key]
Accessing keys & values simultaneously

• Accessing Keys and Values Simultaneously

• for loop is used to iterate all the elements of a dictionary.

access keys : dictionaryName.keys ( )

access values : dictionaryName.values ( )


Characteristics of Dictionary

• Not in Sequence: List, string and tuple all are in sequence


but dictionary is not in a particular sequence. Dictionary is
an unordered collection.
Characteristics of Dictionary

• Keys must be unique: Keys should not be duplicate in


dictionary. They must be unique which used to identify the
values of dictionary. Values can be duplicate but keys can't.
Characteristics of Dictionary

• Mutable: Dictionaries are mutable which means they can be


modified after creation. To change value of respective key
use following dictionaryName [Key] = Value
Characteristics of Dictionary

• Indexed by keys: Keys are used to access the elements of


the dictionary. While lists, tuples and strings use index
number to access the elements
Functions/ Methods

• len(): This method is used to return the length of the


dictionary that counts the number of elements or key -
value pairs present in the dictionary

Syntax :

len (dictionaryName)
Functions/ Methods

• clear ( ): This method is used to remove all the elements


from a dictionary

Syntax :

dictionaryName.clear ( )
Functions/ Methods

• get ( ): This method is used to return the value of respective


key. If key is not present in the dictionary, it will give an error
message

Syntax :

dictionaryName.get (key)
Functions/ Methods

• items ( ): This method is used to return all the elements i.e.


all key value pairs. But, these key value pairs are not in
order.

Syntax :

dictionaryName.get (key)
Functions/ Methods

• update ( ): This method updates the existing dictionary with


new dictionary if needed.

Syntax :

dictionary1.update (dictionary2)
Thank you!

You might also like