Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

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):

        self.goodFood.append(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

        else:

            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']

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
Certainly! In your code, the map() function applies the f.addGoodFood() method to each element in the list ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']. However, since the addGoodFood() method does not have a return statement, map() returns a list of None values: [None, None, None, None, None].

Despite the None values returned by map(), the addGoodFood() method successfully adds the elements to the goodFood list within the FoodExpert instance. That's why when you check f.goodFood separately, you can see that the elements have been added: ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'].

To illustrate this more explicitly, you can achieve the same result by using a for loop instead of map():

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

f = FoodExpert()

f.init()

for food in food_list:

    f.addGoodFood(food)

print(f.goodFood)

This will give you the same output:

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

In this version, each element from food_list is individually passed to the addGoodFood() method in the loop, resulting in the elements being added to f.goodFood without an intermediate list of None values.
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):

    self.goodFood.append(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.

0 votes
by (25.7k points)
In your code, when you use map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']), the map() function is applying the f.addGoodFood() method to each element in the list ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'].

The reason you see [None, None, None, None, None] as the result is because the addGoodFood() method does not have a return statement. Therefore, when map() applies the method to each element, it executes the method but returns None by default since there is no explicit return value.

However, when you check f.goodFood after using map(), you can see that the addGoodFood() method has indeed added the elements to the goodFood list. This is because even though the return value from map() is [None, None, None, None, None], the method addGoodFood() is executed side-effectively, meaning it modifies the goodFood list within the FoodExpert instance.

To demonstrate this more explicitly, you can use a for loop instead of map() to see the modifications:

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

f = FoodExpert()

f.init()

for food in food_list:

    f.addGoodFood(food)

print(f.goodFood)

This will output:

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

Here, each element from food_list is individually passed to addGoodFood() in the loop, resulting in the elements being added to f.goodFood without the intermediate [None, None, None, None, None] list.
0 votes
by (19k points)
In your code, the map() function applies the f.addGoodFood() method to each element in the list ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']. Although map() returns a list of None values, the addGoodFood() method successfully adds the elements to the goodFood list in the FoodExpert instance. Therefore, when you access f.goodFood, you can observe that the elements have been added: ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'].

Alternatively, you can achieve the same result using a for loop:

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

f = FoodExpert()

f.init()

for food in food_list:

    f.addGoodFood(food)

print(f.goodFood)

This produces the identical output:

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

In this version, each element is individually passed to the addGoodFood() method, resulting in the direct addition of elements to f.goodFood without intermediate None values.

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)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...