Python Unit 1 Dict PART 8
Python Unit 1 Dict PART 8
Part 8[Dictionaries]
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 dict.pop(key, default_value)
pop():- removes specific key value pair
Dictionary functions dict.popitem()
popitem():-
– not allow any arguments
– remove last key value pair from the existing dictionary
Dictionary functions dict.clear()
clear():- removes all items from the dictionary
Dictionary functions Dict.get(key, default=None)
Update():-updates the dictionary with the elements from the another dictionary object
Dictionary functions
Standard Type Operators
Merging of Two dictionary objects
dict comprehension