Element-wise addition of 2 lists?
Use map
with operator.add
:
>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]
or zip
with a list comprehension:
>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]
Timing comparisons:
>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop
Element to Element addition of two lists in python
print [i+ j for i in list1 for j in list2]
I think ... if I understand the question right?
if you have
list1 = [1, 2, 3, 4, 5, 6]
list2 = [1, 2, 3, 4, 5, 6]
then this will result in
[ 2,3,4,5,6,7, 3,4,5,6,7,8, 4,5,6,7,8,9, 5,6,7,8,9,10, 6,7,8,9,10,11, 7,8,9,10,11,12]
which sounds like what you want
or do it the cool numpy way
from itertools import chain
l2 = numpy.array(list2)
print chain(*[l2+i for i in list1])
How to sum the elements of 2 lists in python?
you can use zip
/map
:
result = list(map(sum,zip(list1,list2)))
Alternative, via list_comprehension
:
result = [i+j for i,j in zip(list1,list2)]
OUTPUT:
[11, 13, 15, 17, 19]
sum of N lists element-wise python
Just do this:
[sum(x) for x in zip(*C)]
In the above, C
is the list of c_1...c_n
. As explained in the link in the comments (thanks, @kevinsa5!):
*
is the "splat" operator: It takes a list as input, and expands it into actual positional arguments in the function call.
For additional details, take a look at the documentation, under "unpacking argument lists" and also read about calls (thanks, @abarnert!)
Add SUM of values of two LISTS into new LIST
The zip
function is useful here, used with a list comprehension.
[x + y for x, y in zip(first, second)]
If you have a list of lists (instead of just two lists):
lists_of_lists = [[1, 2, 3], [4, 5, 6]]
[sum(x) for x in zip(*lists_of_lists)]
# -> [5, 7, 9]
Element-wise addition of two lists of different lengths?
There's an alternative zip
that does not stop at the shortest: itertools.zip_longest()
. You can specify a fill value for the shorter lists:
from itertools import zip_longest
result = [sum(n) for n in zip_longest(a, b, fillvalue=0)]
Related Topics
How to Run Celery Workers by Superuser
How to Get the Pythonpath in Shell
Visibility of Global Variables in Imported Modules
What Is the Python Equivalent for a Case/Switch Statement
How to Convert Number Words to Integers
How to Profile Memory Usage in Python
Urllib2.Httperror: Http Error 403: Forbidden
How to Import a Module That Is Definitely Installed
Can't Set Attributes on Instance of "Object" Class
How to Pass Another Entire Column as Argument to Pandas Fillna()
Fastest Way to Download 3 Million Objects from a S3 Bucket
Instance Variables VS. Class Variables in Python
Extracting Just Month and Year Separately from Pandas Datetime Column
Using Quotation Marks Inside Quotation Marks
What Is the Reason for Performing a Double Fork When Creating a Daemon
How to Preserve Timezone When Parsing Date/Time Strings with Strptime()
How to Explode a List Inside a Dataframe Cell into Separate Rows