Write a list comprehension that returns all the pairs in a dictionary whose associated values are greater than zero
Suppose the dictionary is -
{'English': 11, 'Aptitude': -3, 'Reasoning': 10, 'GK': -2}
Python has predefined iteritems() method that returns an iterator over the dictionary’s (key, value) pairs.
dictionary.iteritems()
This return the iterator over the dictionary's key value pairs.
This is the following solution to return all the pairs in a dictionary whose associated values are greater than zero.
dict = {'English': 11, 'Aptitude': -3, 'Reasoning': 10, 'GK': -2} for x in dict.iteritems(): if x[1] > 0: print x
('Reasoning' , 10)
('English' , 11 )
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Solution
Python has predefined iteritems() method that returns an iterator over the dictionary’s (key, value) pairs.
Syntax of Python iteritems()
dictionary.iteritems()This return the iterator over the dictionary's key value pairs.
This is the following solution to return all the pairs in a dictionary whose associated values are greater than zero.
Output of the above code
('Reasoning' , 10)
('English' , 11 )
need an explanation for this answer? contact us directly to get an explanation for this answer