Saturday, February 9, 2019

Python Dictionaries

Python dictionaries are unordered collection of data having a key-value pair. Each element in a dictionary is not referenced to an index, but each value has its corresponding key for reference. The elements in a dictionary can be modified and are written within curly brackets.

Creating a dictionary:

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
print(student)

Accessing items in a dictionary:

Since, dictionary items don't have any index, we need to access the items with their index. The following code gets the value "Avinash" of the "name" key and stores it in the variable x.

x = student["name"]
print(x)

We can also use the get() method to do the same thing.

x = student.get("name")
print(x)

Changing values:

We can modify the value of any item using the key of the item.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
student["class"] = "XII"
print(student)

Checking is a key exists in a dictionary:

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
if "section" in student:
    print('The key section is present in the dictionary')

Dictionary Length:

The len() function can be used to find the number of items in a dictionary.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
print(len(student))

Looping through a dictionary:

The following will print all the keys in a dictionary, one by one.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
for x in student:
    print(x)

The following will print all the values in a dictionary, one by one.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
for x in student.values():
    print(x)

We can also use the following way to print all the values, one by one

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
for x in student:
    print(student[x])

To print both key and value of a dictionary, one by one, we can do this

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
for x, y in student.items():
    print(x, y)

Adding items to a dictionary:

We can insert a new key:value pair to a dictionary in the following way

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
student["age"] = 15
print(student)

Removing items from a dictionary:

To remove an item from a dictionary we can use the pop() function.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
student.pop("class")
print(student)

We can also use the del keyword to delete an item in the following way.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
del student["class"]
print(student)

Note: If we use the del keyword only with the dictionary name, it will delete the dictionary completely.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
del student

Clearing the dictionary:

We can use the clear() method to remove all the items in a dictionary.

student = {
    "roll" : 12,
    "name" : "Avinash",
    "class" : "IX",
    "section" : "A"
}
student.clear()
print(student)

.

No comments: