×

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

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

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

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Write a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

5 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python Sys Stdout

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

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

4 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

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