Featured Video Play Icon

Python Dictionaries


Python is a high-level computer programming language of machine learning, deep learning, and data science. It is also multi-paradigm, an object-oriented and structured programming language. Python syntax and semantics are easily readable. In its formatting uses mostly English keywords where other languages use punctuation.

Python Dictionaries

In Python, a dictionary is a series of pairs of things. Each pair contains a key—”first. This is an unordered, changeable, and indexed collection. These are mentioned and written with curly brackets and have keys and values. We declare a particular key and ask what value is paired with it to pick something out of a dictionary.

How to create a Dictionary?

Suppose we’re creating a dictionary named Student_15, with these pairs: the key is “first name”, the value is “Mahmand” the key is “last name”, value is “Mansoor” key is “address”, value is “H.No.3 IFL PP.”

The code would be:

Student_15 = {"first name": "Mahmand", "last name": "Mansoor", "address": “H.No.3 IFL PP "}

This structure is analogous to the code for creating an inventory. Twice starts with a variable name and equal sign. Things are separated by commas. The series is enclosed in curly brackets. Note that the key is followed by a colon: and the variable name is singular, not plural. All values are strings, enclosed in quotation marks, and they are all the keys. Neither keys nor values have to be strings.

How to find information in a Dictionary?

We pick out an element by specifying its key in Dictionary:

address_ of_ student = student_15 ["address"]

In the code above, Python finds the value in the dictionary student_15 that has the key “address” and assigns the string to the variable address_of_student. The string now “H.No.3 IFL PP.” is stored in the variable address_of_student.

If we write…

print (address_of_student)

…Python displays H.No.3 IFL PP.

Keys and values versatility

Keys don’t have to be strings. They can be numbers. For example,

 The code is :  Gradings = {3: "Usman", 1: "Akbar", 4: "Qasim", 2: "Noman"}

In each pair shown above, the key’s variety, not enclosed in quotation marks. We use the number to pick out a value.

first _grading_student = Gradings [1]

Gradings [1] is “Akbar.”

Values can be numbers, too. For example,

student_positions_so_far = {"Qasim": 4, "Akbar": 2, "Noman": 3, "Usman": 1}

We use the key to pick out a value, a string in this case:

Usman_position = student_positions_so_far["Usman"]

student_positions_so_far["Usman"] is 1.

We may mix strings and numbers any way we want.

things_to_remember = {100: "the highest number", "a dozen": 12, "snake eyes": "a pair of ones", 16: "a baker's dozen"}

These are the pairs:

The key is the number 100, value is the string “the highest number” key is the string “a dozen”, value is the number 12 key is the string “snake eyes”, value is the string “a pair of ones” key is the number 16, value is the string “a baker’s dozen”

How to add a key-value pair in a dictionary?

In the dictionary student_15 has these pairs… the key is “first name”, the value is “Mahmand” the key is “last name”, the value is “Mansoor” the key is “address”, value is ” H.No.3 IFL PP.”

We may mix a new pair by writing… student_15 [“city”] = “Faisalabad”

In the above-mentioned code, we have those familiar pieces—the dictionary name, the key in square brackets, and the value. They’re just in a different order.

Now the dictionary:  student_15 has these pairs…key is “first name”, value is “Mahmand” key is “last name”, value is “Mansoor” key is “address”, value is ” H.No.3 IFL PP.” key is “city”, value is “Faisalabad”

We can also define an empty dictionary, a dictionary with no key-value pairs:

players_of_team = { }

Later, we would fill the dictionary with pairs, adding one at a time:

How to change the value of an element in Dictionary?

It begins with the name of the dictionary. Then comes the key in the dictionary in square brackets. Then the assignment of the new value. For example,

del student_15[“address”]

Looping through values in Dictionaries

We can loop through a dictionary by employing a for loop..

for each_value in student_15.values( ):


In the next comes a variable to store the value for each iteration.

for each_value in student_15.values( ):


Note: each_value is a variable. We can give it any legal variable name we like. Next, the keyword is followed by the name of the dictionary, student_15: Then a dot… Then the keyword values… …then empty parentheses… …and a colon…The code that tells Python what action(s) to require whenever through the loop. This code is indented:

Looping through keys in Dictionaries

We do write the keyword keys… instead of the keyword values

for each_key in student_15.keys( ):


Looping through key value pairs in Dictionaries

This is the dictionary we’ve been working with:

The code: Student_15 = {“first name”: “Mahmand”, “last name”: “Mansoor”, “address”: “H.No.3 IFL PP “}

Now the code for looping through the dictionary and printing all the keys and values:

for each_key, each_value in student_15.items():

The code: print(“The student’s ” + each_key + ” is ” + each_value)

Following the instructions above, Python displays:

The student’s first name is Mahmand

The customer’s last name is Mansoor

The customer’s address is H.No.3 IFL PP.

How to create a list of Dictionaries?

If we required more than one dictionary for each student in our described example, each dictionary represents a single student and contains his or her first name, last name, and address. Let’s create a list of three dictionaries, one for each of three students. The code will be:

The dictionary : Students = [{ "student id": 0, "first name":"Ahmed", "last name": "Tanveer", "address": "P.O. Box 2114.", }, {"customer id": 1, "first name" :"Amir", "last name": "Ali", "address": "PO Box 3265", }, {"customer id": 2, "first name":"Faisal", "last name": "Karim", "address": "H.No.5 Karachi.", }, ]

How to determine the length of the Dictionary?

We use the len() function to determine how many items (key-value pairs) are there in a dictionary. For example, print the number of items in the dictionary:


How to copy a Dictionary?

We use the built-in Dictionary method copy().to makes a copy. For example,

My_car = { "brand": "Cultus", "model": " VXR", "year": 2009}

His_car = My_car.copy()


For more Python details:https://www.technologiesinindustry4.com/2020/10/AI-Python-Range-Function.html#more

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top