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

Finding the addends of a sum in Python

I have a list of values, and I get the sum of how many elements are in the list by using len(). How do I go about retrieving the addends of a sum? I’m trying to plot the list, and I want to get the amount of numbers as I’m plotting students and ages.

Code:

numbers = ['14', '17', '14', '15', '16', '15', '15', '14', '18', '15', '16', '14', '14']
ageNum = [int(item) for item in numbers]

addends = []
print(addends)

# addend finding code
# expected output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

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 :

You can use range on the len of the input:

numbers = ['14', '17', '14', '15', '16', '15', '15', '14', '18', '15', '16', '14', '14']
addends = list(range(len(numbers)))

output:

>>> addends
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
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