Adding All Elements of Two Lists

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

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]

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]

obtain the sum of all the elements in two lists in python

https://docs.python.org/3/library/functions.html#sum

You could write this code as:

v = sum(od) + sum(ed)

EDIT: As a side note, the way you're doing item iteration is not 'pythonic' as well. On most languages you access array items using indexes (the [i] part), but on python, the ideal way to iterate an array is by getting the values directly.

So, if you wanted to follow the same structure as the initial code, the 'pythonic' way to write it would be something like this:

result = 0

for value in od:
result += value

for value in ed:
result += value

print(result)

Adding all elements of two lists

Something like

   mapply("+",e1,e2)

works for the first part ...

Reduce( function(x,y) mapply("+",x,y),ee)[[3]]

There may be something even slicker. Reduce doesn't take a ... argument so we can't get away with Reduce(mapply,ee,FUN="+")[[3]]

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 add two lists together, avoid repetitions, and order elements?

Try this:

combined = [list1, list2]
union = list(set().union(*combined))

This takes advantage of the predefined method (.union()) of set() , which is what you need here.

combined can have as many elements inside it, as the asterisk in *combined means that the union of all of the elements is found.

Also, I list()ed the result but you could leave it as a set().

As @glibdud states in the comments, it's possible that this might produce a sorted list, but it's not guaranteed, so use sorted() to ensure that it's ordered. (like this union = sorted(list(set().union(*combined))))

Adding elements of two lists

You are adding two strings, that's why '55' + '51' = '5551'.

Cast them to integers in order to sum the two numbers:

list3 = [(int(x) + int(y)) for x, y in zip(list1, list2)]

How do i add two lists' elements into one list?

You can use list comprehensions with zip:

list3 = [a + b for a, b in zip(list1, list2)]

zip produces a list of tuples by combining elements from iterables you give it. So in your case, it will return pairs of elements from list1 and list2, up to whichever is exhausted first.



Related Topics



Leave a reply



Submit