×

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 is Sleeping Time in Python

Did you ever postpone a Python program's execution? Usually, you want your code to execute as quickly as possible. But there are times when it is in your best interests...

3 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Get Bounding Box Co-ordinates Python

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....

6 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Spark and Python for big data with pyspark github

Pyspark: Apache Spark is a computational engine that handles large data sets using parallel and batch processing. Apache Spark is an open-source, distributed computing framework and collection of libraries for real-time,...

3 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 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.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

Difference between Perl and Python

Control and presently utilized for a great many undertakings, including framework organization, web improvement, network programming, and GUI improvement, and the sky is the limit from there. About Perl? Perl is a...

4 minutes read.