Using functools.partial() with map():

The issue is given belwo

File “d:\2024-academic\Async\zip.py”, line 39, in
print ( map(population_percentage, abc, 58000))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ‘int’ object is not iterable

The issue here that facing is because of passing an int (58000) directly as an argument to the map() function, which expects iterables for both abc and total. Instead, you can pass the total as a single value by modifying the map() call slightly.

We need to pass the total population (58000) as an argument inside the function. One approach is to use a lambda or functools.partial() to include the total population in the map function.

Here’s the corrected code:

from functools import partial

def population_percentage(abc : tuple, total: int) -> int:
        print('aaaaa', abc)
        key, value = abc
        perc=  ((value/total) * 100)

        return (key, perc)




# Use functools.partial to fix 'total' argument
total_population = 58000
partial_population_percentage = partial(population_percentage, total=total_population)

print ( "map", list(map(partial_population_percentage, abc)))

Leave a Reply

Your email address will not be published. Required fields are marked *

Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/wp-includes/formatting.php on line 4720