Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading