Question
they are 2 pictures attached
4) Delete set of keys from Python Dictionary Given: sampleDict = { name: Kelly, age:25, salary : 8000, city: New y

1) Write a Python program to iterate over dictionaries using for loops d = {X: 10, y: 20, z: 30) 2) Write a Python progra
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Delete set of keys from Python Dictionary

Images of the Code:

1 #Delete set of keys from Python Dictionary 2 #initializing Dictionary 3 sampleDict = { 4 name: Kelly, 5 age:25, 6 sa
Note: If the below code is missing indentation please refer code Images

Typed Code:

#Delete set of keys from Python Dictionary
#initializing Dictionary
sampleDict = {
"name": "Kelly",
"age":25,
"salary": 8000,
"city": "New york"
}
#keys to remove
keysToRemove = ["name", "salary"]
#for loop will iterate 2 times because keysToRemove has 2 elements
#for every iteration, one value from keysToRemove is assigned to i
for i in keysToRemove:
#if that i value is in sampleDict
if i in sampleDict:
#Delete the value
del sampleDict[i]
#printing the sampleDict
print(sampleDict)
//code ended here

Output:

{city: New york, age: 25}

Iterate over dictionaries using for loops

Images of the Code:

1 #Iterate over dictionaries using for Loops 2 d = {X:1, y:2, z:3} 3 #for Loop will iterate 3 times because 3 items in
Note: If the below code is missing indentation please refer code Images

Typed Code:

#Iterate over dictionaries using for loops   
#Initializing Dictionary
d = {'x':1, 'y':2, 'z':3}
#for loop will iterate 3 times because 3 items in d
#for every iteration, one key and one value is assigned to key,value
#sorted is used to sorts the items in an order
for key,value in sorted(d.items()):
#printing key and value
print(key,"->",value)
//code ended here

Output:

x -> 1 y -> 2 z -> 3

get maximum and minimum values in a Dictionary

Images of the Code:

1 #get maximum and minimum values in a Dictionary 2 my_dict = {x:500, y:5874, z:560} 3 #taking values to val val = my_d
Note: If the below code is missing indentation please refer code Images

Typed Code:

#get maximum and minimum values in a Dictionary
my_dict = {'x':500, 'y':5874, 'z':560}
#taking values to val
val = my_dict.values()
#finding Maximum value in it
maximum = max(val)
#finding Minimum value in it
minimum = min(val)
#printing Maximum and Minimum value
print("Maximum value: ",maximum)
print("Minimum value: ",minimum)
//code ended here

Output:

Maximum value: 5874 Minimum value: 500

change Brad's salary to 8500

Images of the Code:

1 #change Brads salary to 8500 2 #initializing Dictionary 3 sampleDict = { 4 empl: {name: Jhon, salary: 7500}, 5 emp
Note: If the below code is missing indentation please refer code Images

Typed Code:

#change Brad's salary to 8500
#initializing Dictionary
sampleDict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 6500}
}
#changing salary to 8500
sampleDict['emp3']['salary'] = 8500
#printing sampleDict
print(sampleDict)
//code ended here

Output:{empl: {name: Jhon, salary: 7500}, emp2: {name: Emma, salary: 8000}, emp3: {name: Brad, salary: 8500}

If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!

Add a comment
Know the answer?
Add Answer to:
they are 2 pictures attached 4) Delete set of keys from Python Dictionary Given: sampleDict =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys()...

    Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict....

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT