0% found this document useful (0 votes)
10 views2 pages

Python Dictionary Operations and Examples

The document contains Python programs for various dictionary operations. It includes swapping keys and values, displaying students with marks greater than 50, and filtering employees based on salary. Additionally, it provides commands for traversing a dictionary, printing values, deleting elements, clearing the dictionary, and adding new elements.

Uploaded by

sonaliasutkar85
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Python Dictionary Operations and Examples

The document contains Python programs for various dictionary operations. It includes swapping keys and values, displaying students with marks greater than 50, and filtering employees based on salary. Additionally, it provides commands for traversing a dictionary, printing values, deleting elements, clearing the dictionary, and adding new elements.

Uploaded by

sonaliasutkar85
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write the program

a. Swap keys and values of a dictionary


Example : if dictionary has d={'a':1,'b':2} then swapped dictionary will have
{1: 'a', 2: 'b'}

Program:-
d={'a':1,'b':2}

s = {}

for key,value in [Link]():


s[value] = key

print(s)

b. Consider a dictionary D={‘Amit’:[50,’XI’],’Kamal’:[90,’XII’],’Ravi’:[80,’XII’]}


Which stores name as key and marks and class as value.
Write a program to display the name of those students who obtained more than 50
marks.
Output should be ‘Kamal’ and ‘Ravi’ because they scored more than 50 marks.

Program:-

D={'Amit':[50,'XI'],'Kamal':[90,'XII'],'Ravi':[80,'XII']}

for key, value in [Link]():


if value[0] > 50:
print(key)

Q. Write a program to create a dictionary with eid, name and salary of 5 of employees and
display names of employees who have got more than 25000 salary.

Program:-

employees = {
101: {"name": "Alice", "salary": 30000},
102: {"name": "Bob", "salary": 20000},
103: {"name": "Charlie", "salary": 35000},
104: {"name": "David", "salary": 18000},
105: {"name": "Eve", "salary": 40000},
}

# Displaying names of employees with salary greater than 25000


print("Employees with salary greater than 25000:")
for eid, details in [Link]():
if details["salary"] > 25000:
print(details["name"])

Q. Consider the following dictionary:


d={1:‟One‟,3:‟Three‟,5,‟Five‟,7:‟Seven‟,9:‟Nine‟}
Perform the below mentioned operations in the dictionary:
(a)Write command traverse a dictionary and print key & values
(b)Write command to print only values of dictionary.
(c)Write command to delete the second last element of dictionary.
(d)Write command to delete all elements of the dictionary
(e)write command to add another element with key as 33 and value 23

# Given dictionary
d = {1: "One", 3: "Three", 5: "Five", 7: "Seven", 9: "Nine"}

# (a) Traverse the dictionary and print keys & values


print("Keys and Values:")
for key, value in [Link]():
print(f"Key: {key}, Value: {value}")

# (b) Print only the values of the dictionary


print("\nValues in the dictionary:")
for value in [Link]():
print(value)

# (c) Delete the second last element of the dictionary


# Find the second last key using list
keys = list([Link]())
second_last_key = keys[-2]
del d[second_last_key]
print("\nDictionary after deleting the second last element:")
print(d)

# (d) Delete all elements of the dictionary


[Link]()
print("\nDictionary after deleting all elements:")
print(d)

# (e) Add another element with key as 33 and value as 23


d[33] = 23
print("\nDictionary after adding key 33 with value 23:")
print(d)

You might also like