Python Dictionary

Dictionary in Python is a collection of keys values, used to store data values like a map, which, unlike other data types which hold only a single value as an element.
A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
Dictionary holds key:value pair. Key-Value is provided in the dictionary to make it more optimized.
Dictionaries are written with curly brackets, and have keys and values.
Example:

dic = {1:"hello",2:"world"}
print(dic)
dic2 = {1:"learn",
        2:"python",
        3:"rguktweb"}
print(dic2)
print(type(dic))
print(type(dic2))
Creating a Dictionary

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’.
Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value.
Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable.
Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}.
Example:

newdict = {}
print(newdict) ## empty dictionary
newdict =  {1:"hello",
            2:"world",
            3:"welcome",
            4:"to",
            5:"rguktweb"}
print(newdict)
print(type(newdict))
newdict2 = dict([(1,"hello"),
                (2,"world"),
                (3,"welcome"),
                (4,"to"),
                (5,"rguktweb")])
print(newdict2)
print(type(newdict2))

Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly.

Adding elements to a Dictionary

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.
Example:

x = {}
x[0] = "aaa"
x[1] = "bbb"
x[2] = "ccc"
print(x)
x[1] = "ddd"
print(x)

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.

Accessing elements of a Dictionary

In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets.
There is also a method called get() that will also help in accessing the element from a dictionary.This method accepts key as argument and returns the value.
Example:

x = {1:"a",2:"b",3:"c",4:"d",5:"e"}
print(x[1]) ## returns the key value
print(x[3])
print(x[4])
a = x.get(5)
print(a)
Changing Dictionary Items

We can change the value of a specific item by referring to its key name.
The update() method will update the dictionary with the items from the given argument.
The argument must be a dictionary, or an iterable object with key:value pairs.
Example:

l = {1:"abcd",
     2:"pqrs",
     3:"wxyz",
     4:"klmn"}
print(l)
l[2]= "abcd"
print(l)
l.update({4:"abcd"})
print(l)