How do I add indefinite number of list together and output a single list? [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
Better way to iterate over two or multiple lists at once [duplicate]
2 answers
The inputs will be lists (which is the number of list is indefinite), the function is supposed to iterate through all the index and add each value of the list for same index for all the inputted list mathematically together. The output will be the a list which consist of all the added values
For example:
lista = [1,2,3] listb = [2,3,5] listc = [-3,2,1]
outputlist = [0,7,9]
My function below is only able to add 2 list together, I want no restrictions as to how many list. How do I do that?
Thank you very much in advance
def listadd(a,b):
counter = 0
list =
while counter < len(a):
list.append(a[counter]+b[counter])
counter += 1
return list
python
marked as duplicate by jonrsharpe, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 15:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
up vote
1
down vote
favorite
This question already has an answer here:
Better way to iterate over two or multiple lists at once [duplicate]
2 answers
The inputs will be lists (which is the number of list is indefinite), the function is supposed to iterate through all the index and add each value of the list for same index for all the inputted list mathematically together. The output will be the a list which consist of all the added values
For example:
lista = [1,2,3] listb = [2,3,5] listc = [-3,2,1]
outputlist = [0,7,9]
My function below is only able to add 2 list together, I want no restrictions as to how many list. How do I do that?
Thank you very much in advance
def listadd(a,b):
counter = 0
list =
while counter < len(a):
list.append(a[counter]+b[counter])
counter += 1
return list
python
marked as duplicate by jonrsharpe, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 15:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You know the python zip function?
– quant
Nov 11 at 15:22
Please include more information about expected inputs/outputs.
– Jim Stewart
Nov 11 at 15:22
See e.g. stackoverflow.com/q/919680/3001761
– jonrsharpe
Nov 11 at 15:22
Side note: never (even as an example) shadow built-ins, e.g. uselst
,L
orlist_
instead oflist
as your argument name.
– jpp
Nov 11 at 15:26
Sorry for not being specific in the question, I meant to add each values of the same list index mathematically, not combining the list.
– Royston Teo
Nov 11 at 15:34
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
Better way to iterate over two or multiple lists at once [duplicate]
2 answers
The inputs will be lists (which is the number of list is indefinite), the function is supposed to iterate through all the index and add each value of the list for same index for all the inputted list mathematically together. The output will be the a list which consist of all the added values
For example:
lista = [1,2,3] listb = [2,3,5] listc = [-3,2,1]
outputlist = [0,7,9]
My function below is only able to add 2 list together, I want no restrictions as to how many list. How do I do that?
Thank you very much in advance
def listadd(a,b):
counter = 0
list =
while counter < len(a):
list.append(a[counter]+b[counter])
counter += 1
return list
python
This question already has an answer here:
Better way to iterate over two or multiple lists at once [duplicate]
2 answers
The inputs will be lists (which is the number of list is indefinite), the function is supposed to iterate through all the index and add each value of the list for same index for all the inputted list mathematically together. The output will be the a list which consist of all the added values
For example:
lista = [1,2,3] listb = [2,3,5] listc = [-3,2,1]
outputlist = [0,7,9]
My function below is only able to add 2 list together, I want no restrictions as to how many list. How do I do that?
Thank you very much in advance
def listadd(a,b):
counter = 0
list =
while counter < len(a):
list.append(a[counter]+b[counter])
counter += 1
return list
This question already has an answer here:
Better way to iterate over two or multiple lists at once [duplicate]
2 answers
python
python
edited Nov 11 at 15:32
asked Nov 11 at 15:20
Royston Teo
264
264
marked as duplicate by jonrsharpe, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 15:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jonrsharpe, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 15:22
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You know the python zip function?
– quant
Nov 11 at 15:22
Please include more information about expected inputs/outputs.
– Jim Stewart
Nov 11 at 15:22
See e.g. stackoverflow.com/q/919680/3001761
– jonrsharpe
Nov 11 at 15:22
Side note: never (even as an example) shadow built-ins, e.g. uselst
,L
orlist_
instead oflist
as your argument name.
– jpp
Nov 11 at 15:26
Sorry for not being specific in the question, I meant to add each values of the same list index mathematically, not combining the list.
– Royston Teo
Nov 11 at 15:34
|
show 1 more comment
You know the python zip function?
– quant
Nov 11 at 15:22
Please include more information about expected inputs/outputs.
– Jim Stewart
Nov 11 at 15:22
See e.g. stackoverflow.com/q/919680/3001761
– jonrsharpe
Nov 11 at 15:22
Side note: never (even as an example) shadow built-ins, e.g. uselst
,L
orlist_
instead oflist
as your argument name.
– jpp
Nov 11 at 15:26
Sorry for not being specific in the question, I meant to add each values of the same list index mathematically, not combining the list.
– Royston Teo
Nov 11 at 15:34
You know the python zip function?
– quant
Nov 11 at 15:22
You know the python zip function?
– quant
Nov 11 at 15:22
Please include more information about expected inputs/outputs.
– Jim Stewart
Nov 11 at 15:22
Please include more information about expected inputs/outputs.
– Jim Stewart
Nov 11 at 15:22
See e.g. stackoverflow.com/q/919680/3001761
– jonrsharpe
Nov 11 at 15:22
See e.g. stackoverflow.com/q/919680/3001761
– jonrsharpe
Nov 11 at 15:22
Side note: never (even as an example) shadow built-ins, e.g. use
lst
, L
or list_
instead of list
as your argument name.– jpp
Nov 11 at 15:26
Side note: never (even as an example) shadow built-ins, e.g. use
lst
, L
or list_
instead of list
as your argument name.– jpp
Nov 11 at 15:26
Sorry for not being specific in the question, I meant to add each values of the same list index mathematically, not combining the list.
– Royston Teo
Nov 11 at 15:34
Sorry for not being specific in the question, I meant to add each values of the same list index mathematically, not combining the list.
– Royston Teo
Nov 11 at 15:34
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can use map
with zip
:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])
1
Or with a list comprehension for increased readability:return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
1
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use map
with zip
:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])
1
Or with a list comprehension for increased readability:return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
1
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
add a comment |
up vote
2
down vote
accepted
You can use map
with zip
:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])
1
Or with a list comprehension for increased readability:return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
1
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use map
with zip
:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])
You can use map
with zip
:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])
answered Nov 11 at 15:25
jpp
86.7k194998
86.7k194998
1
Or with a list comprehension for increased readability:return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
1
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
add a comment |
1
Or with a list comprehension for increased readability:return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
1
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
1
1
Or with a list comprehension for increased readability:
return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
Or with a list comprehension for increased readability:
return [sum(values) for values in zip(*lsts)]
– soundstripe
Nov 11 at 15:41
1
1
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
docs.python.org/3/library/functions.html#zip
– soundstripe
Nov 11 at 15:44
add a comment |
You know the python zip function?
– quant
Nov 11 at 15:22
Please include more information about expected inputs/outputs.
– Jim Stewart
Nov 11 at 15:22
See e.g. stackoverflow.com/q/919680/3001761
– jonrsharpe
Nov 11 at 15:22
Side note: never (even as an example) shadow built-ins, e.g. use
lst
,L
orlist_
instead oflist
as your argument name.– jpp
Nov 11 at 15:26
Sorry for not being specific in the question, I meant to add each values of the same list index mathematically, not combining the list.
– Royston Teo
Nov 11 at 15:34