Python Dictionaries

python tutorial

Python Fundamentals – Python Dictionaries.

Python Fundamentals – Python Dictionaries

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

What is Dictionary?

A dictionary is a collection which 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

G C Reddy

G C Reddy, Working as a Software Testing Trainer in Hyderabad, India.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *