Friday, February 2, 2024

How to Use "Dictionary Data" type in Python

 Python is a programming language with syntax that closely resembles human language. This language can be used for various purposes, including web development, application creation, machine learning development, and much more. In this article, I will discuss how to manage dictionary data types in Python.

What is a Dictionary Data Type?

In Python, there is a dictionary data type that functions to store data in a key and value format. In other programming languages like JavaScript, this data type is similar to an object. An example of a dictionary data type can be seen in the code below.

user = {

    'name': 'John Doe',

    'age': 20,

    'role': 'admin'

}

The above code shows a variable containing a dictionary. Dictionary data types can be created within curly braces, and data can be stored in a key-value format. In the example above, one of the key-value pairs is "name" as the key, and "John Doe" as the value for the "name" key.


Displaying Dictionary Data Type

To retrieve all the contents of a dictionary data type, you can simply call its variable name, as shown in the following code.

print(user)

To retrieve a specific piece of data, you can call its key name as shown below.

print(user['name'])

In the above code, we call the variable with the name "user" containing a dictionary data type, and then we call the key with the name "name." After that, we get the value of the "name" key.

Adding New Key-Value Pair

New data can be added to a dictionary data type using code like the following.

user = {

    'name': 'John Doe',

    'age': 20,

    'role': 'admin'

}

user['city'] = 'Jakarta'

In the above code, we add new data to the variable "user" with the key "city" and its value as "Jakarta."


Deleting Data

To delete specific data in a dictionary data type, it is very straightforward, as shown in the code below.

del user["role"]

In the above code, we delete data with the key "role" from the variable "user" by simply writing "del."


Looping through a Dictionary Data Type

Sometimes, there are cases where we want to process data in a dictionary data type using a loop. This can be done as shown in the example code below.

user = {

    'name': 'John Doe',

    'age': 20,

    'role': 'admin'

}



for key, value in user.items():

    print(f'Key : {key} , Value : {value}')

In the above case, we loop through the "user" variable, and there is a slight difference when looping through a dictionary data type. We call the items() function, and during each iteration, we get two pieces of data: key and value. The result of the above code will look like the following:

Key: name, Value: John Doe

Key: age, Value: 20

Key: role, Value: admin

I hope you understand and grasp the concept of dictionary data types after reading this article. For more details, you can refer to the existing documentation. Thank you for reading to the end. If you have any suggestions or feedback, feel free to comment below. Thank you, and see you in the next article.

0 comments:

Post a Comment