Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (12.7k points)

I can't see how the map() is functioning behind the hood, for what reason is it returning a rundown with all None, except for when I check f.goodFood the components have been added there?

class FoodExpert:

    def init(self):

        self.goodFood = []

    def addGoodFood(self, food):


    def likes(self, x):

        return x in self.goodFood

    def prefers(self, x, y):

        x_rating = self.goodFood.index(x)

        y_rating = self.goodFood.index(y)

        if x_rating > y_rating:

            return y


            return x

I also wrote this code, after declaring this class:

>>> f = FoodExpert()

>>> f.init()

>>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'])

[None, None, None, None, None]

>>> f.goodFood

['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']

1 Answer

0 votes
by (26.4k points)

Here, map applies a capacity/function on an iterable and returns another list where the function was applied on every item. 

For your situation, it shows None on the grounds that f.addGoodFood work brings nothing back. 

For testing purposes change addGoodFood along these lines:

def addGoodFood(self, food):


    return "test"

and see:

>>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'])

['test', 'test', 'test', 'test', 'test']

Interested to learn python in detail? Come and Join the python course.

Related questions

0 votes
1 answer
asked Jul 6, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)

Browse Categories