Q:

Write a list comprehension that returns all the pairs in a dictionary whose associated values are greater than zero

0

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}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

dict = {'English': 11, 'Aptitude': -3, 'Reasoning': 10, 'GK': -2}
for x in dict.iteritems():
    if x[1] > 0:
        print x
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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now