Hello guys. Welcome back to another exciting session on python. Hope you have been having a blast while learning programming from this platform. If you are new here, please do checkout our previous articles and subscribe to our YouTube channel to get a better understanding of what we are going to do today.
Today, we are going to do one of the last in-built data types in python called python dictionary. Just as in the other in-built data types, you will learn about python dictionaries, how they are created, accessing, removing elements from and its different in-built data-types.
A python dictionary is an unordered group of items. It can contain any data type and is changeable. An item in a dictionary contains a key-value pair.
My_dict={key:value1, key:value2, key:value3}
Accessing elements on a dictionary
Since a dictionary is unordered, we can’t use the index method to access items. We are going to use keys:values instead or the get method (). But in this session we are going to use the key[]inside square brackets.
Example
my_dict={'name':'mark','age':24,'ranking':'5th'}
x=my_dict['name']
print(x)
OUTPUT
We are going to use the assignment =operator to modify and add the value of existing keys. When we assign a key:value to a dictionary, the compiler checks if the key exist or not and if it does, the value simply gets updated.
Example
my_dict={'name':'mark','age':24,'ranking':'5th'}
my_dict['status']='regular'
print(my_dict)
OUTPUT
We are going to use the pop method to remove items from a dictionary. Note that they are many other methods which we can use to remove items from a dictionary. The pop() methods remove the value of the pair and returns the value of the given key. Let’s look at an example
my_dict={'name':'mark','age':24,'ranking':'5th'}
my_dict.pop('age')
print(my_dict)
OUTPUT
The operators ‘in’ and ‘not in’ is used to check a specific key exist on the dictionary. Take note that this test is only for dictionary keys not for values.
my_dict={2:'GRS',4:'XYZ',6:'JKL'}
print(4 in my_dict)
print(7 in my_dict)
OUTPUT
You can use the ‘for’ loop to iterate through a dictionary. For example
my_dict={2:'GRS',4:'XYZ',6:'JKL'}
for value in my_dict:
print(value)
OUTPUT
As we have with the other data types, they are many types of in-built functions which can be used to perform specific task.
lens()
It gives the total number of items on a dictionary
dict_one={2:'GRS',4:'XYZ',6:'JKL'}
x=len(dict_one)
print(x)
OUTPUT
We are going to create new dictionary from the python iterable. It is usually made up for a key:value expression and a’for’ statement put inside a curly braces. Following is an example that shows how you can build a dictionary key-value pairs of a range of numbers.
cube={x:x**3 for x in range(5)}
x=cube
print(x)
OUTPUT
The item()method
dict_items()
Values() method
dict_values()
Keys()method
dict_keys()
Copy method
Dict_copy()
Hope the last session was very interesting. Happy coding guys.
Comments
Post a Comment
Please do not enter any spam link in the comment box.