Python Forum
How to separate the values in an element to add them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to separate the values in an element to add them
#1
Greetings,

I have a python question.. I have a list with the following four elements.
What I want to do is add the vales in element 1 (17,21,29,35,36) to get the sum (138)
The area I am having the problem is how add the numbers in that element?
The numbers are all part of element 1, how would I separate them in to integers so I can add them to get the sum?

Here is an example of a list.
['01/21/2023', '17,21,29,35,36', '1,E','37.43']

Any help or guidance would be greatly appreciated.

Regards,
Monty
Reply
#2
This would be one way:

the_list = ['01/21/2023', '17,21,29,35,36', '1,E', '37.43']

numbers = the_list[1].split(',')
total = 0

for number in numbers:
    total += int(number)

print(total)
Or, you could do that directly:

the_list = ['01/21/2023', '17,21,29,35,36', '1,E', '37.43']
total = 0
for number in the_list[1].split(','):
    total += int(number)
print(total)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Wow!! Thank you Rob101! I spent a week trying to figure this out.. I was close.
I was using the split but I was getting the comma between each value..
Reply
#4
(Jan-23-2023, 01:04 AM)monty024 Wrote: Wow!! Thank you Rob101!

You're welcome; pleased to help.

Yet another way, I just thought of:
the_list = ['01/21/2023', '17,21,29,35,36', '1,E', '37.43']

int_numbers = []
for number in the_list[1].split(','):
    int_numbers.append(int(number))

print(sum(int_numbers))
Edit to add: it depends on what you want to do with the numbers. If you want to use them later on, then maybe use my 3rd way, as you'll have a list object that holds said numbers, but if all you want is the total, then there's little point in creating an object to hold them.
BashBedlam likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
Lots of ways to do this. Here are a 3 more.
from functools import reduce

values = '1,2,3,4,5'

print(sum(int(n) for n in values.split(',')))
print(sum(map(int, values.split(','))))
print(reduce(lambda a, b: a + int(b), values.split(','), 0))
I presented the choices in my order of preference. Comprehensions are very useful and something you need to know how to use. map() is like a one-trick-pony version of a comprehension. I don't find reduce() very useful.
Larz60+ likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,410 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Unable to locate element no such element gahhon 6 4,558 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Looping through dictionary and comparing values with elements of a separate list. Mr_Keystrokes 5 3,946 Jun-22-2018, 03:08 PM
Last Post: wavic
  Comparing values in separate lists KaleBosRatjes 3 3,060 May-02-2018, 04:38 PM
Last Post: KaleBosRatjes
  Change single element in 2D list changes every 1D element AceScottie 9 12,149 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020