Python Dictionary
Python Dictionary
Note – Dictionary keys are case sensitive, the same name but different cases of Key
will be treated distinctly.
print(Dict)
print(Dict)
Output:
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary can also be created by the built-in function dict(). An empty dictionary can
be created by just placing to curly braces{}.
Dict = {}
print(Dict)
print(Dict)
print(Dict)
Output:
Empty Dictionary:
{}
print(Dict)
Output:
In Python Dictionary, the Addition of elements can be done in multiple ways. One
value at a time can be added to a Dictionary by defining value along with the key e.g.
Dict[Key] = ‘Value’. Updating an existing value in a Dictionary can be done by using
the built-in update() method. Nested key values can also be added to an existing
Dictionary.
Note- While adding a value, if the key-value already exists, the value gets updated
otherwise a new Key with the value is added to the Dictionary.
Dict = {}
print(Dict)
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print(Dict)
Dict['Value_set'] = 2, 3, 4
print(Dict)
Dict[2] = 'Welcome'
print(Dict)
print(Dict)
Output:
Empty Dictionary:
{}
In order to access the items of a dictionary refer to its key name. Key can be used
inside square brackets.
# Creating a Dictionary
print(Dict['name'])
print(Dict[1])
Output:
Accessing a element using key:
For
# Creating a Dictionary
print(Dict.get(3))
Output:
Accessing a element using get:
Geeks
In order to access the value of any key in the nested dictionary, use indexing [] syntax.
# Creating a Dictionary
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name'])
Output:
{1: 'Geeks'}
Geeks
For
In Python Dictionary, deletion of keys can be done by using the del keyword. Using
the del keyword, specific values from a dictionary as well as the whole dictionary can
be deleted. Items in a Nested dictionary can also be deleted by using the del keyword
and providing a specific nested key and particular key to be deleted from that nested
Dictionary.
Note: The del Dict will delete the entire dictionary and hence printing it after deletion
will raise an Error.
# Initial Dictionary
print(Dict)
del Dict[6]
print(Dict)
del Dict['A'][2]
Output:
Initial Dictionary:
{'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 'B': {1: 'Geeks', 2:
'Life'}, 5: 'Welcome', 6: 'To', 7: 'Geeks'}
Pop() method is used to return and delete the value of the key specified.
# Creating a Dictionary
pop_ele = Dict.pop(1)
Output:
Dictionary after deletion: {3: 'Geeks', 'name': 'For'}
Value associated to poped key is: Geeks
Using popitem() method
The popitem() returns and removes an arbitrary element (key, value) pair from the
dictionary.
# Creating Dictionary
pop_ele = Dict.popitem()
Output:
Dictionary after deletion: {3: 'Geeks', 'name': 'For'}
The arbitrary pair returned is: (1, 'Geeks')
All the items from a dictionary can be deleted at once by using clear() method.
# Creating a Dictionary
Dict.clear()
print(Dict)
Output:
Deleting Entire Dictionary:
{}
Dictionary Methods
Methods Description
They copy() method returns a shallow copy of the
dictionary.
copy()
The clear() method removes all items from the dictionary.
clear()
Removes and returns an element from a dictionary having
the given key.
pop()
Removes the arbitrary key-value pair from the dictionary
and returns it as tuple.
popitem()
It is a conventional method to access a value for a key.
get()
returns a list of all the values available in a given dictionary
dictionary_name.values() .
Produces a printable string representation of a dictionary.
str()
Adds dictionary dict2’s key-values pairs to dict
update()
Set dict[key]=default if key is not already in dict
setdefault()
Returns list of dictionary dict’s keys
keys()
Returns a list of dict’s (key, value) tuple pairs
items()
Returns true if key in dictionary dict, false otherwise
has_key()
Create a new dictionary with keys from seq and values set to
value.
fromkeys()
Returns the type of the passed variable.
type()
Compares elements of both dict.
cmp()