How to sum certain numbers in a python list?
up vote
-2
down vote
favorite
I have a list that contains [date,data1,data2,data3...].
Now I need to sum all the data but not the date. How could I do that?
I hope I could get a new list that looks like [date,data1+data2+data3...]
python list sum
New contributor
add a comment |
up vote
-2
down vote
favorite
I have a list that contains [date,data1,data2,data3...].
Now I need to sum all the data but not the date. How could I do that?
I hope I could get a new list that looks like [date,data1+data2+data3...]
python list sum
New contributor
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have a list that contains [date,data1,data2,data3...].
Now I need to sum all the data but not the date. How could I do that?
I hope I could get a new list that looks like [date,data1+data2+data3...]
python list sum
New contributor
I have a list that contains [date,data1,data2,data3...].
Now I need to sum all the data but not the date. How could I do that?
I hope I could get a new list that looks like [date,data1+data2+data3...]
python list sum
python list sum
New contributor
New contributor
New contributor
asked 2 days ago
Vera
31
31
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
You can use list slicing to get all of the original list except the first element. Then you can pass the resulting slice to sum()
.
>>> original = ['date',11,222,3]
>>> summed = [original[0], sum(original[1:])]
>>> print(summed)
['date', 236]
dict will be good to use here
– Hackaholic
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can use list slicing to get all of the original list except the first element. Then you can pass the resulting slice to sum()
.
>>> original = ['date',11,222,3]
>>> summed = [original[0], sum(original[1:])]
>>> print(summed)
['date', 236]
dict will be good to use here
– Hackaholic
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
add a comment |
up vote
4
down vote
accepted
You can use list slicing to get all of the original list except the first element. Then you can pass the resulting slice to sum()
.
>>> original = ['date',11,222,3]
>>> summed = [original[0], sum(original[1:])]
>>> print(summed)
['date', 236]
dict will be good to use here
– Hackaholic
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can use list slicing to get all of the original list except the first element. Then you can pass the resulting slice to sum()
.
>>> original = ['date',11,222,3]
>>> summed = [original[0], sum(original[1:])]
>>> print(summed)
['date', 236]
You can use list slicing to get all of the original list except the first element. Then you can pass the resulting slice to sum()
.
>>> original = ['date',11,222,3]
>>> summed = [original[0], sum(original[1:])]
>>> print(summed)
['date', 236]
edited 2 days ago
answered 2 days ago
Robᵩ
113k12129212
113k12129212
dict will be good to use here
– Hackaholic
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
add a comment |
dict will be good to use here
– Hackaholic
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
dict will be good to use here
– Hackaholic
2 days ago
dict will be good to use here
– Hackaholic
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
That may be true, but OP specifically asked for a list.
– Robᵩ
2 days ago
add a comment |
Vera is a new contributor. Be nice, and check out our Code of Conduct.
Vera is a new contributor. Be nice, and check out our Code of Conduct.
Vera is a new contributor. Be nice, and check out our Code of Conduct.
Vera is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238300%2fhow-to-sum-certain-numbers-in-a-python-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password