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]
>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]