Python Dictionary :

Posted by

What is a dictionary and what is it’s use in python programming, python dictionary is a another data type where we store data in terms of key value pair.

You might have heard this dictionary in iOS swift programming language and also similar way of storing data in key value pairs in android too called as Map in android.

In our previous tutorial on python set we have seen that set is denoted by { } and elements in between and set avoids repeated elements the same scenario for dictionary.

A dictionary is a built-in data structure in Python that stores a collection of key-value pairs. Each key-value pair is known as an item, and the keys in a dictionary must be unique and immutable. The values can be of any data type, including lists, tuples, and even other dictionaries.

Dictionary is also represented by { } with key – value pair avoiding duplicate elements.You can create a dictionary in Python using curly braces {} or by calling the dict() constructor.

Python Dictionary Video Tutorial :

For detailed video tutorial may visit below

Storing data to python dictionary :

user = { 'name' : 'abhi', 'age' : 25 }

user ( try to print user )

{ 'name' : 'abhi', 'age' : 25 }

Fetching data form dictionary :

You can specify the key and fetch value

user[ 'name' ]

abhi

user [ 'age' ]

25

we can also make use of get() to fetch the value as

user.get( 'name' )

abhi

Adding data to dictionary :

user [ 'email' ] = "abc@mail.com"

now print user

{ ‘name’ : ‘abhi’, ‘age’ : 25, ’email’ : ‘abc@mail.com’ }

Deleting data from dictionary :

del user [ 'age' ]

{ ‘name’ : ‘abhi’, ’email’:’abc@mail.com’ }

Python Dictionary Usage :

Using dictionary we can fetch the data much faster and easier also we can avoid duplicate values in this way so mostly we use dictionary for fetching api data.

Python dictionaries are widely used in many applications, including web development, data science, machine learning, and more. Here are some common use cases for Python dictionaries:

Storing and retrieving data :

Dictionaries are often used to store and retrieve data in a key-value format. For example, a web application might use a dictionary to store user information, with the user’s email address as the key and a dictionary of user data as the value.

Counting occurrences :

Dictionaries can be used to count the occurrences of elements in a list or string. For example, you could use a dictionary to count the number of times each word appears in a text document.

Configuring settings :

Dictionaries can be used to store configuration settings for an application. For example, a web application might use a dictionary to store the database credentials or other settings.

Grouping data :

Dictionaries can be used to group data by a certain attribute. For example, you could use a dictionary to group a list of students by their grade level.

Caching data :

Dictionaries can be used to cache data that is expensive to compute. For example, a web application might use a dictionary to cache the results of a database query so that subsequent requests can be served more quickly.

Implementing graphs :

Dictionaries can be used to implement graphs in Python. Each node in the graph can be a key in the dictionary, and the value can be a list of adjacent nodes.

Parsing and manipulating JSON :

JSON (JavaScript Object Notation) is a popular format for exchanging data between web services. Dictionaries can be used to parse and manipulate JSON data in Python.

Overall, Python dictionaries are a versatile and powerful tool that can be used in a wide variety of applications.

If you have any query’s in this tutorial on python dictionary do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

python dictionary
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x