Skip to main content

Python Dictionary

Python dictionary

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.

How a python dictionary is created.
Each item in a dictionary is placed inside curly braces {} and separated by a colon(:).
Syntax
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

Adding or modifying entries to a dictionary.

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

Removing or deleting elements from a dictionary.

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

Dictionary membership test.

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

Iterating Through a Dictionary.

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

Using built-in functions with dictionary

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

Dictionary comprehension

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

Take note that are other python dictionary methods which can be used to perform other perform various python task in python. Some of these methods are 

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

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...