×

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as a component, Dictionary holds the key: value pair. Key: Value is given in the Dictionary to make it more advanced and useful.

It is generally used when we have a large amount of data. It is a necessary to know the key to restoring its value. A dictionary key can be almost any Python data (except a list) but is usually preferred as a number or string. Dictionaries are defined in braces, with each item separated by key: value. In one Dictionary, we can have different types of keys and values. Word recommendations are advanced to recover values when the key is known. Python dictionary gives us many other interesting strategies assisting us while performing tasks.

Example

      d={1:"abc",2:"xyz","pqr:10"}

Creating a dictionary in python

Creating a dictionary is simple: just putting data inside curly braces {} separated by commas. A data has a key and values; while the qualities can be of any data or information type and can review, keys should be of unchanging kind (string, number, or tuple with permanent components) and should be exceptional.

NOTE: Dictionary keys are case sensitive, a similar name yet various key occurrences will be dealt with particularly.

#creating an empty dictionary

d1=dict{}

This is how we can create an empty dictionary.

Example

info = {'name': 'Md Shibbu', 'age': 19, 'location': 'Noida'}
print(info)
print(type(info))

Output

Dictionary in python

Example 2

k = ['rupees', 'pencils', 'copies', 'mugs'];
v = [13, 4, 39, 7];
items = dict(zip(k, v))
print(items)

Output

Dictionary in python

Addicting elements to a dictionary

In Python Dictionary, the extension of components should be possible in more than one way. Each value, in return, can be added to a Dictionary by identifying values alongside the key, for example, Dict[Key] = 'Value'. Settled key values can also be added to a current Dictionary. Refreshing a current value in a Dictionary should be possible using the implicit update() method.

Example

d = {'India':200,'Nepal':300,'Bhutan':400}
print("Country Name",d)
d.update({'India':500})
print("updated country name",d)

Output

Dictionary in python

Example 2

d = {'Shyam':200,'Rahul':300,'Rohit':400}
print("Name",d)
d.update({'Rahul':600,'Rohit':900})
print("updated name",d)

Output

Dictionary in python

NOTE: If the key is already present, the current value gets refreshed. A new (Key: value) match is added to the Dictionary if the key is absent.

Removing an element from a dictionary in python

We know that Dictionary is mutable; we can eliminate a specific thing in a dictionary using the pop() method. This method eliminates a thing with the given key and returns the value. The pop item() method can eliminate and return a changeable (key, value) thing pair from the Dictionary. All things can be eliminated immediately by using the del() method. We can utilize the del()keyword to eliminate individual things or the whole Dictionary itself.

Example

bdict = {'a': 'anil', 'b': 'baibhav', 'c': 'crypto', 'd': 'daniel'}
print(bdict)
ae =bdict.pop('c')
print(bdict)
print('Removed element: ', ae)

Output

Dictionary in python

Example 2

bdict = {'a': 'Amir', 'b': 'Baibhav', 'c': 'crypto', 'd': 'daniel'}
print(bdict)
if "c" in bdict:
    del bdict["c"]


print("Updated Dictionary :", bdict)

Output

Dictionary in python

Methods in dictionary in python

In Python, as with strings and lists, a few implicit techniques can be appealed on word references. Now and again, the overview and word reference techniques share a similar name. In the conversation on object-oriented programming, you will see that it is completely ok for various kinds to have approached with a similar name.

clear()- Eliminate every element from the dictionary

copy()- Returns a duplicate value of the Dictionary

get() - Returns the value of determined key

items() - Returns an overview containing a tuple for each key-value pair

keys() - Returns an overview containing the Dictionary's keys

Pop() - Remove the item and stores it for future use

pop item() - Removes the last inserted key-value pair in dictionary

update() - Updates dictionary with determined key-value pairs

values() - Returns an overview of the values of the Dictionary

NOTE: The .things(), .keys(), and .values() methods really return something many refer to as a view object. A dictionary view object is like a window on the keys and values. For down-to-earth purposes, you can consider these techniques returning arrangements of the Dictionary's keys and values.

Conclusion

In Python,Lists and dictionaries are two of the most often utilized Python types. In Dictionary, data and word references are two of the most often utilized Python types. As you have seen, they have a few similarities yet differ in how their components are used. Data and word references will generally be fitting for various conditions.


Related Topics

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python vars() Function

Python vars() Function The vars() function in Python returns the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute. Syntax vars([object]) Parameter object: This parameter represents any object with a __dict__attribute Return This function returns...

1 minute read.

Sklearn linear Model in Python

The most effective and reliable Python machine learning library is Sklearn. Through a Python consistency interface, it offers a variety of effective tools for statistical modeling and machine learning, including...

5 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

How to slice a list in python

When working with lists, we face situations where we may need a part of the list from one index to the other. Slicing is one of the simpler ways to...

5 minutes read.

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We...

3 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Create Table Using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's...

4 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.