for my code i want all numbers from a dictionary under 70 to be deleted, i'm unsure of how to specify this and i need it to also delete the associated name with that number as well, either that or only diplay numbers that are 70 or above. below is the code that i have in it's entirety:
name = []
number =[]
name_grade = {}
counter = 0
counter_bool= True
num_loop = True
while counter_bool:
stu = int(input("please enter the number of students: "))
if stu < 2:
print("value is too low, try again")
continue
else:
break
while counter != stu:
name_inp = str(input("Enter your name: "))
while num_loop:
number_inp = int(input("Enter your number: "))
if number_inp < 0 or number_inp > 100:
print("The value is too high or too low, please enter a number between 0 and 100.")
continue
else:
break
name_grade[name_inp] = number_inp
name.append(name_inp)
number.append(number_inp)
counter = counter 1
print(name_grade)
sorted_numbers = sorted(name_grade.items(), key= lambda x:x[1])
print(sorted_numbers)
if number > 70:
resorted_numbers = number < 70
print(resorted numbers)
how would i go about this?
Also if it's also not too much trouble could someone explain in detail about dictionary keys and how the lambda function I've used works? i got help but I would prefer to know the small details on how it's applied and formatted but don't worry if it's a pain to explain.
CodePudding user response:
You can just iterate over the dictionary and filter for values less than 70:
resorted_numbers = {k:v for k,v in name_grade.items() if v<70}
dict.items
method returns a list of key-value tuple pairs of a dictionary, so the lambda function is telling the sorted
function to sort by the second element in each tuple.