1
a = [1,2,3]
b = [4,5,6]

I want the output to be [5,7,9]

What is the simplest syntax that can achieve this in pure Python 3?

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
Mike
  • 58,961
  • 76
  • 175
  • 221
  • And the uglier version of @BuddyBob's answer : `c = list(map(sum, zip(a, b)))` – S.B May 28 '21 at 18:29
  • 1
    Yes I like your answer @BuddyBob. I will accept it shortly if nothing nicer comes along :) – Mike May 28 '21 at 19:32

2 Answers2

7

I'd say using zip() would be a nice way

a = [1,2,3]
b = [4,5,6]
c = [x+y for x,y in zip(a,b)]

output

[5, 7, 9]
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
0

Use zip to pair the data, then several ways

  • c = [x + y for x, y in zip(a, b)]
  • c = [sum(pair) for pair in zip(a, b)]
  • c = list(map(sum, zip(a, b)))
azro
  • 53,056
  • 7
  • 34
  • 70