1

Can you add two lists element by element with out iterating through it as below? This works, but eh. I miss matlab.

list1 = [3, 7, 3]
list2 = [4, 5, 6]

for x in range(0, len(list1)): 
   list1[x] = list1[x] + list2[x]

print (list1)

Output :
[7, 12, 9]
2
  • 2
    "I miss matlab." Use numpy then... (and btw all methods will iterate over your lists, the question is whether it is hidden under the hood or not...) Commented Nov 22, 2017 at 4:21
  • Yes, but some methods are optimized because they are performed a lot.That is more of what I am asking. Commented Nov 22, 2017 at 4:37

5 Answers 5

3

Don't miss Matlab at all :) The numpy arrays are your typical Matlab arrays:

arr1 = np.array([3, 7, 3])
arr2 = np.array([4, 5, 6])
arr1 + arr2

gives you what you want.

If you want to stick with lists, you can do this:

list1 = [3, 7, 3]
list2 = [4, 5, 6]
[sum(x) for x in zip(list1, list2)]

After the performance comments, especially Patrick's comparison, I did a comparison myself, and verified that numpy is actually the fastest among the current solutions posted. However, there is another solution which beats numpy! Let's see:

# Numpy:
timeit arr1 + arr2 # 1.67 µs per loop
# List comprehension:
timeit [sum(x) for x in zip(list1, list2)] # 2.59 µs per loop
timeit [(x+y) for x,y in zip(list1,list2)] # 1.72 µs per loop
# Map and Reduce-based (note that reduce is really overkill, I did it just for fun):
timeit map(sum, zip(list1, list2)) # 2.58 µs per loop
timeit map(lambda a, b: a + b , list1, list2) # 2.11 µs per loop
timeit [reduce((lambda x,y: x+y), e) for e in zip(list1,list2)] # 4.05 µs per loop
# And the winner:
from operator import add
timeit map(add, list1, list2) # 1.57 µs per loop
Sign up to request clarification or add additional context in comments.

Comments

1

zip the lists together, and sum the pairs

list(map(sum, zip(list1, list2)))

EDIT:

I did some basic testing, and the overwhelming winners were the list comprehension if the input data starts as lists, and numpy if you can have it as arrays before your operation

def test1(list1, list2):
  l=[]
  for x in range(0, len(list1)):
    l.append(list1[x] + list2[x])
  return l    

def test2(list1, list2):
  return list(map(sum, zip(list1, list2)))

def test3(list1, list2):
  return [x+y for x, y in zip(list1, list2)]

import numpy as np

def test4(list1, list2):
  return np.array(list1) + np.array(list2)

def test5(list1, list2):
  return list1 + list2

from timeit import timeit

print(timeit('test1(list1, list2)', setup='list1 = list(range(100)); list2 = list(range(100))', globals=globals()))
print(timeit('test2(list1, list2)', setup='list1 = list(range(100)); list2 = list(range(100))', globals=globals()))
print(timeit('test3(list1, list2)', setup='list1 = list(range(100)); list2 = list(range(100))', globals=globals()))
print(timeit('test4(list1, list2)', setup='list1 = list(range(100)); list2 = list(range(100))', globals=globals()))
print(timeit('test4(list1, list2)', setup='list1 = np.array(list(range(100))); list2 = np.array(list(range(100)))', globals=globals()))
print(timeit('test5(list1, list2)', setup='list1 = np.array(list(range(100))); list2 = np.array(list(range(100)))', globals=globals()))

Gives me

15.42712744511664 # append
17.329718918073922 # my solution above
7.0252319818828255 # list comprehension
16.53089915495366 # numpy with list inputs
1.430903600063175 # numpy with array inputs that are double checked
0.6451617309357971 # numpy assuming array inputs

These are the times in seconds to execute the operation 1000000 times.

Numpy is surprisingly slow when forced to do np.array(list) every time.

Try it yourself at this repl.it

4 Comments

Is that going to be more efficient then the for loop?
The most efficient way as I think is using numpy. See Fatih's answer.
@zippo I threw together a little time trial to see what's actually fastest.
Thats awesome! Really answered my question!
1

If you want matlab-like behavior, you should use numpy

In [5]: list1 = [3, 7, 3]

In [6]: list2 = [4, 5, 6]

In [7]: import numpy as np

In [8]: np.array(list1) + np.array(list2)
Out[8]: array([ 7, 12,  9]

Comments

1

You can also use list comprehension over both the lists

list1 = [3, 7, 3]
list2 = [4, 5, 6]
list3 = [(x+y) for x,y in zip(list1,list2)]
print(list3)

Output:

[7, 12, 9]

Comments

1

You can use a lambda and a map:

>>> list1 = [3, 7, 3]
>>> list2 = [4, 5, 6]
>>> list(map(lambda a, b: a + b , list1, list2))
[7, 12, 9]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.