Python Dictionaries

Dictionaries in Python, What is Python Dictionary?, Data Structures in Python, Create a Dictionary, and Python Dictionary Methods.

A useful data type built into Python is the dictionary. Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.

Python Data Structures – Dictionaries

1. Accessing Dictionary Items
2. Change a Values
3. Print a Dictionary
4. Check if a Key Exists
5. Find Dictionary Length
6. Adding Items to a Dictionary
7. Removing Items
8. Copy a Dictionary
9. Nested Dictionaries
10. The dict() Constructor

What is Dictionary?

A dictionary is a collection that is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values.

Example:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}
print(man )

Operations on Dictionaries

1. Accessing Dictionary Items

We can access the items of a dictionary by referring to its key name, inside square brackets:

Example:

Get the value of the “age” key:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

x = man[“age”]
print (x)

2. Change Values

We can change the value of a specific item by referring to its key name:

Example:

Change the “age” to 45:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

man [“age”] = 45

print(man)

3. Print Dictionary

a. Print all (key and value pairs)

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}
print(man)

b. Print Keys

Print all key names in the dictionary, one by one:

for x in man:
print(x)

c. Print Values

Print all values in the dictionary, one by one:

for x in man:
print(man[x])

or

for x in man.values():
print(x)

d. Loop through both keys and values, by using the items() function:

for x, y in man.items():
print(x, y)

4. Check if Key Exists

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

if “name” in man:
print(“Yes, ‘name’ is one of the keys in the man dictionary”)

5. Find Dictionary Length

Dictionary Length

Print the number of items in the dictionary:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

print(len(man))

6. Adding Items Dictionary

Adding an item to the dictionary is done by using a new index key and assigning a value to it:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

man[“color”] = “white”
print(man)

7. Removing Items

There are several methods to remove items from a dictionary:

Examples

a. The pop() method removes the item with the specified key name:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

man.pop(“study”)
print(man)

b. The popitem() method removes the last inserted item

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}
man.popitem()
print(man)

c. The del keyword removes the item with the specified key name:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

del man[“age”]
print(man)

d. The del keyword can also delete the dictionary completely:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

del man
print(man) # Error

e. The clear() keyword empties the dictionary:

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

man.clear()
print(man)

8. Copy a Dictionary

There are ways to make a copy, one way is to use the built-in Dictionary method copy().

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

newman= man.copy()
print(newman)

Another way to make a copy is to use the built-in method dict().

man = {
“name”: “Venkat”,
“study”: “Engineering”,
“age”: 24
}

newman = dict(man)
print(newman)

9. Nested Dictionaries

10. The dict() Constructor


Python Step by Step Tutorials

Python Video Tutorial

Follow me on social media:

1 thought on “Python Dictionaries”

Comments are closed.