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

How to use all the values of a list and use it in an equation?

I’ve posted my first question here which is also linked to this question: Link

How do I use all the values in a list containing values e.g. [10,20,30,40,50] and use it in an equation?

price = [10,20,30,40,50]
total_balance = (balanceA + balanceB) * price

I’m hoping it would use each element inside the list as the value for the variable price and would result in a list (total_balance) that contains the result computed for each element inside that price list using the equation. I’ve googled questions similar to mine but they only use one value inside the list.

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

>Solution :

Just loop through the list:

price = [10,20,30,40,50]
total_balance = []
for p in price: # Go through every value in the price list
    tmp_price = (balanceA + balanceB) * p # You calculate the price with the selected value here
    total_balance.append(tmp_price) # Here you add your calculated price to your list with all prices
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