Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python dictionaries

What is python dictionary?

Dictionary is used to store multiple collections of values in one variable. We can store too many value in a dictionary and perform a lot of work on it. Dictionary is unordered, changeable, and does not allow duplicates. We can store string, number, or both at the same time in a dictionary. We can use a capital letter and a small letter in a dictionary.

How to create Dictionaries?

Dictionary is written inside the second bracket. So first give the name of the dictionary. We can't give space between two words of a dictionary name. Then give an equal sign and then a second bracket{}.
Dictionary is used to store data values in key: value pairs.
For string must use single or double quotations (' '," ") and for numbers no need to use single or double-quotes.
Give a comma between two values like (key: values, key: value).
Key means name of the value. If we want to access a single value from the dictionary then we have to call the value by its key name. key name can't be double in a dictionary and to access the full dictionary, just call the dictionary name.

Example:1
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
print(x)
Output
{'name': 'SelfTaught', 'Number': 2141343, 'topic': 'learn python'}

In the example, x is the dictionary name. Name, Number, and topic are the key names and what we wrote in front of every key are the key values.

Example:2
Input
x={
  "name":"SelfTaught",
  "Number":"1.2141343"
                  "2.23232323"
                  "3.3233334",
  "topic":  "learn python\n"
                  "Learn Data science\n"
                  "Learn Web development\n"
                  "learn data analyst\n"
                  "Learn game development"
}
print(x)
Output
{'name': 'SelfTaught', 'Number': '1.21413432.232323233.3233334', 'topic': 'learn python\nLearn Data science\nLearn Web development\nlearn data analyst\nLearn game development'}

Some dictionary function

get() function

To get a specific values from a dictionary get() function is used.

Example:
Input
x={
  "name":"SelfTaught",
  "Number":"1.2141343"
                  "2.23232323"
                  "3.3233334",
  "topic":  "learn python\n"
                  "Learn Data science\n"
                  "Learn Web development\n"
                  "learn data analyst\n"
                  "Learn game development"
}
print(x.get("topic"))
print(x.get("name"))
Output
learn python
Learn Data science
Learn Web development
learn data analyst
Learn game development

{"name":"SelfTaught"}

Another way to get the values of a dictionary

Example:
Input
x={
  "name":"SelfTaught",
  "Number":"1.2141343"
                  "2.23232323"
                  "3.3233334",
  "topic":  "learn python\n"
                  "Learn Data science\n"
                  "Learn Web development\n"
                  "learn data analyst\n"
                  "Learn game development"
}
v=x["topic"]
print(v)
Output
learn python
Learn Data science
Learn Web development
learn data analyst
Learn game development

keys() function

The keys function will make a list of all the keys present in the dictionary.

Example:
Input
x={
  "name":"SelfTaught",
  "Number":"1.2141343"
                  "2.23232323"
                  "3.3233334",
  "topic":  "learn python\n"
                  "Learn Data science\n"
                  "Learn Web development\n"
                  "learn data analyst\n"
                  "Learn game development"
}
v=x.keys()
print(v)
Output
dict_keys(['name', 'Number', 'topic'])

values() function

values() function will make a list of all the values present in the dictionary.

Example:
Input
x={
  "name":"SelfTaught",
  "Number":"1.2141343"
                  "2.23232323"
                  "3.3233334",
  "topic":  "learn python\n"
                  "Learn Data science\n"
                  "Learn Web development\n"
                  "learn data analyst\n"
                  "Learn game development"
}
v=x.values()
print(v)
Output
dict_values(['SelfTaught', '1.21413432.232323233.3233334', 'learn python\nLearn Data science\nLearn Web development\nlearn data analyst\nLearn game development'])

How to add new key:value?

Example:
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
x["add_new_name_1"]="SelfTaught again" "5423466",
x["add_new_name_2"]="SelfTaught awesome\n" "5423475"
print(x)
Output
{'name': 'SelfTaught', 'Number': 214134334356, 'topic': 'learn python', 'add_new_name_1': ('SelfTaught again5423466',), 'add_new_name_2': 'SelfTaught awesome\n5423475'}

In the example we added two new keys which names are add_new_name_1, add_new_name_2. These two keys and values will be added at the end of the dictionary.

update() function

To update value of a dictionary update() function is used.

Example:
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
y={topic: "learn python update"}
x.update(y)
print(x)
Output
{'name': 'SelfTaught', 'Number': 2141343, 'topic': 'learn python update'}

In the example value of topic --> "learn python" is updated to learn python update .

pop() function

To remove key and value from dictionary pop() function is used.

Example:
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
x.pop("name")
print(x)
Output
{'Number': 2141343, 'topic': 'learn python'}

popitem() function

To remove the last key and value from a dictionary popitem() function is used. If you use the popitem function one time then it will remove one value. Every time you do that, the value of the total will decrease from the end.

Example:1
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
x.popitem()
print(x)
Output
{'name': 'SelfTaught', 'Number': 2141343}
Example:2
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
x.popitem()
x.popitem()
print(x)
Output
{'name': 'SelfTaught'}

copy() function

To copy one dictionary to another dictionary copy() function is used.

Example:
Input
x={
  "name":"SelfTaught",
  "Number":2141343,
  "topic": "learn python"
}
y=x.copy()
print(y)
Output
{'name': 'SelfTaught', 'Number': 2141343, 'topic': 'learn python'}

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.