Dictionary Lab Guide
Dictionary Lab Guide
Think of a Python dictionary like a magical bag where you can store lots of items, but instead
of just putting them inside randomly, you give each item a special tag so you can find it
easily later.
Bag: Imagine you have a special bag that can hold many things. This bag is like a Python
dictionary.
Items and Tags: Inside the bag, you can put different items like toys, books, snacks, etc. But
here's the cool part: for each item you put in, you also attach a tag to it. This tag is a special
label that describes what the item is or helps you remember where you put it.
Finding Things: Now, when you want to find something in your bag, you don't need to search
through everything. You just look at the tag, and it tells you exactly where to find the item
you're looking for.
In Python, these "items" are called keys, and the "tags" attached to them are called values. So, a
Python dictionary is like a magical bag where you store pairs of keys and values. When you want to
find a value, you just need to know its corresponding key.
Step 2: Open the file in editor of your choice (VS Code, notepad++, or notepad)
Syntax:
In the above dictionary Dict, The keys Name and Age are the strings which comes under the category
of an immutable object.
Output
Example 2:
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Hcl', 2: 'WIPRO', 3:'Facebook'})
print("\nCreate Dictionary by using dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(4, 'Rinku'), (2, Singh)])
print("\nDictionary with each item as a pair: ")
print(Dict)
Output
Example 3:
Employee = {"Name": "Dev", "Age": 20, "salary":45000,"Company":"WIPRO"}
print(type(Employee))
print("printing Employee data .... ")
print("Name : %s" %Employee["Name"])
print("Age : %d" %Employee["Age"])
print("Salary : %d" %Employee["salary"])
print("Company : %s" %Employee["Company"])
Output
Example - 1:
Output
Example - 2:
Output
The items of the dictionary can be deleted by using the del keyword as given below.
Example 1:
Output
Deleting Elements using pop() Method
Example 2:
# Creating a Dictionary
Dict1 = {1: 'JavaTpoint', 2: 'Educational', 3: 'Website'}
# Deleting a key
# using pop() method
pop_key = Dict1.pop(2)
print(Dict1)
Output
Example 1
Output
Example 2
Output
Example - 3
#for loop to print the values of the dictionary by using values() method.
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"WIPRO"}
for x in Employee.values():
print(x)
Output
Example 4
#for loop to print the items of the dictionary by using items() method
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"WIPRO"}
for x in Employee.items():
print(x)
Output
In the dictionary, we cannot store multiple values for the same keys. If we pass more than one value
for a single key, then the value which is last assigned is considered as the value of the key.
Example 1:
Employee={"Name":"John","Age":29,"Salary":25000,"Company":"WIPRO","Name":
"John"}
for x,y in Employee.items():
print(x,y)
Output
len()
The dictionary's length is returned via the len() function in Python. The string is lengthened
by one for each key-value pair.
Example 1:
Output
any()
Like how it does with lists and tuples, the any() method returns True indeed if one dictionary
key does have a Boolean expression that evaluates to True.
Example 1:
Output
all()
Unlike in any() method, all() only returns True if each of the dictionary's keys contain a True
Boolean value.
Example 1:
Output
sorted()
Like it does with lists and tuples, the sorted() method returns an ordered series of the
dictionary's keys. The ascending sorting has no effect on the original Python dictionary.
Example 1:
Output
Example 1:
# dictionary methods
dict = {1: "Hcl", 2: "WIPRO", 3: "Facebook", 4: "Amazon", 5: "Flipkart"}
# clear() method
dict.clear()
print(dict)
Output
copy()
Example 1:
# dictionary methods
dict = {1: "Hcl", 2: "WIPRO", 3: "Facebook", 4: "Amazon", 5: "Flipkart"}
# copy() method
dict_demo = dict.copy()
print(dict_demo)
Output
pop()
# dictionary methods
Example 1:
Output
popitem()
# dictionary methods
Example 1:
Output
keys()
Example 1:
# dictionary methods
dict = {1: "Hcl", 2: "WIPRO", 3: "Facebook", 4: "Amazon", 5: "Flipkart"}
# keys() method
print(dict.keys())
Output
items()
# dictionary methods
Example 1:
Output
get()
Example 1:
# dictionary methods
dict = {1: "Hcl", 2: "WIPRO", 3: "Facebook", 4: "Amazon", 5: "Flipkart"}
# get() method
print(dict.get(3))
Output
update()
it mainly updates all the dictionary by adding the key-value pair of dict2 to this dictionary.
# dictionary methods
Example 1:
Output
values()
It returns all the values of the dictionary with respect to given input.
# dictionary methods
Example 1:
Output