Search code examples
pythonlist-comprehension

TypeError: 'int' object is not iterable with zip()


the question goes like this:

revenue = [30000, 50000, 70000, 90000]
cost = [10000, 15000, 20000, 30000]

Write a comprehension together with the zip() function to make a new list that maintains profits.

I tried doing it with for-loop:

new_list = []
for revenue, cost in zip(revenue, cost):
    profit = revenue - cost
    new_list.append(profit)

new_list

output:

[20000, 35000, 50000, 60000]

which is my desired output, but I cannot do it this way as I must use comprehension.

So, when I tried coding it with comprehension:

zipped_list = list(zip(revenue, cost))
[revenue - cost for num1, num2 in zipped_list]

I get the error message of:

TypeError: 'int' object is not iterable.

How can I solve this?


Solution

  • This should do the work:

    revenue = [30000, 50000, 70000, 90000]
    cost = [10000, 15000, 20000, 30000]
    output = [rev - cost for rev, cost in zip(revenue, cost)]
    

    OUTPUT:

    [20000, 35000, 50000, 60000]