Mapping and Set Types
▪ Mapping Type: Dictionaries
– Operators
– Built-in Functions
– Built-in Methods
– Dictionary Keys
▪ Set Types
– Operators
– Built-in Functions
– Built-in Methods
Dictionary
▪ Dictionary is an unordered set of Key : Value pairs, key are unique
▪ A pairs of braces creates an empty dictionary {}
▪ Duplicate keys are not allowed but values can be duplicated
▪ Heterogenous object are allowed for both key and values
▪ Insertion order is not preserved
▪ Keys are immutable and values are mutable
▪ Dynamic
▪ Indexing and slicing concept are not applicable
Create dictionary
1. Creating empty dictionary and adding key : value pairs
2. Creating a dictionary with dict() function
3. Creating a dictionary with curly braces including key : values pairs
Creating empty dictionary and adding key : value pairs
Creating a dictionary with dict() function
Creating a dictionary with curly braces including key : values pairs
Accessing data from dictionary
▪ We can assign values to the keys and later we can fetch values by using keys
has_key()
In Python 2
Adding new key: values pairs
Updating dictionary values
Deleting Key : value pairs from dictionary
Dictionary functions
▪ Keys():- Returns all keys from dict
Dictionary functions
▪ Values():- return all values
Dictionary functions
▪ Copy()
Dictionary functions [Link](key, default_value)
▪ pop():- removes specific key value pair
Dictionary functions [Link]()
▪ popitem():-
– not allow any arguments
– remove last key value pair from the existing dictionary
Dictionary functions [Link]()
▪ clear():- removes all items from the dictionary
Dictionary functions [Link](key, default=None)
▪ Get():-used to get the value of specified key
Dictionary functions [Link]()
▪ items():- A view object that displays a list of a given dictionary’s (key, value)
tuple pair.
Dictionary functions [Link]([other])
▪ Update():-updates the dictionary with the elements from the another dictionary object
Dictionary functions
Standard Type Operators
Merging of Two dictionary objects
dict comprehension