2

I have a list of lists and would like to operate on the items elementwise, combining items with the same index.

a = [[1,2,3],
     [4,5,6],
     [7,8,9]]

# or
b = [['1', '2', '3'],
     ['4', '5', '6'],
     ['7', '8', '9']]

Operate on a[0][i], a[1][i], a[2][i].

new = [f(a[0][0],a[1][0]a[2][0]),
       f(a[0][1],a[1][1],a[2][1]),
       f(a[0][2],a[1][2],a[2][1])]

Where f is any function that accept appropriate arguments.


Attempt to make a canonical for multiple similar questions. Feel free to add answers that I have overlooked.

wwii
  • 23,232
  • 7
  • 37
  • 77
  • Provide an example of what you want. It really depends on your application more than anything: list size, number of elements, the intended operation, etc. – Mad Physicist Jan 05 '20 at 19:34
  • 1
    @MadPhysicist I'll suffer the downvotes. I got tired of trying to find dupes for this question. I wanted an answer I could propose for similar questions. If I get too much negative feedback, I'll delete. – wwii Jan 05 '20 at 19:37
  • Sounds good. The problem I see here is that your propose is to create a question that's too broad and use it to close questions that aren't. But that's just my take on it. – Mad Physicist Jan 05 '20 at 20:48
  • … exactly, The variety of specifics can make it hard to find an actual duplicate even though there is a similar way to solve them all. This is the first time I tried something like this - it might not work. Hopefully I'll be able to point to it for a plethora of questions. – wwii Jan 05 '20 at 22:55
  • @wwii I just saw the example you shared, can you clarify something: The title and body contain the word _elementwise_, yet both the example in your post and the one you shared are operations on the "columns" of the 2D list. Can you expand on what you mean? – AMC Jan 05 '20 at 23:02
  • Just to clarify something, the target list is always a list of lists? – AMC Jan 07 '20 at 20:33

1 Answers1

1

Start by using zip to combine the items.

Multiple lists.

>>> list(zip(['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']))
[('1', '4', '7'), ('2', '5', '8'), ('3', '6', '9')]

Transposing a lists of list using zip(*thelist) - rows,columns -> columns,rows -

a = [[1,2,3],
     [4,5,6],
     [7,8,9]]

>>> list(zip(*a))
[(1, 4, 7),
 (2, 5, 8),
 (3, 6, 9)]

b = [['1', '2', '3'],
     ['4', '5', '6'],
     ['7', '8', '9']]

>>> list(zip(*b))
[('1', '4', '7'),
 ('2', '5', '8'),
 ('3', '6', '9')]

Once you have done that you can iterate and combine. All have a similar pattern: call a function/method passing the combined values.

Summing numbers

>>> [sum(thing) for thing in zip(*a)]
[12, 15, 18]
>>> list(map(sum, zip(*a)))
[12, 15, 18]

adding/concatenating/joining strings

>>> [''.join(thing) for thing in zip(*b)]
['147', '258', '369']
>>> list(map(''.join, zip(*b)))
['147', '258', '369']

Combining list of strings with list of numbers.

>>> x = ['a', 'b', 'c']
>>> y = [1,2,3]
>>> z = ['g', 'e', 'h']
>>> [''.join(str(item) for item in thing) for thing in zip(x,y,z)]
['a1g', 'b2e', 'c3h']
>>> [''.join(map(str,thing)) for thing in zip(x,y,z)]
['a1g', 'b2e', 'c3h']

Apply function to list of list elementwise.

>>> def f(args):
...     a,b,c,*_ = args
...     return (a+b)**c

>>> [f(thing) for thing in zip(*a)]
[78125, 5764801, 387420489]
wwii
  • 23,232
  • 7
  • 37
  • 77
  • 1
    You could also use `pandas` or `numpy` if your eventual goal is data manipulation – Ajmal Jan 05 '20 at 19:30
  • 3
    I welcome you to add an answer using Pandas based examples I wanted to concentrate on Python only answers. – wwii Jan 05 '20 at 19:38
  • @wwii Do you think you can share some posts to demonstrate the kind of questions for which you’re creating this canonical answer? – AMC Jan 05 '20 at 22:58
  • @AMC - here is the one that prompted me today - https://stackoverflow.com/questions/59602241/how-to-add-element-for-element-in-a-list-of-list – wwii Jan 05 '20 at 23:00
  • @wwii That's not an elementwise operation though, no? Am I missing something? – AMC Jan 07 '20 at 15:53
  • @amc - ahh well, struggling with the terminology - many of the questions of this nature use that term, even though it might be misleading - can you suggest an alternative? – wwii Jan 07 '20 at 16:17
  • @wwii I don't know, that will be difficult without seeing more of the questions which you consider to be of the same style. You're really the person most likely to know :p – AMC Jan 07 '20 at 20:32
  • _Apply function to list of list elementwise._ I should have specified that that isn't an element wise operation, I think it's a column wise operation. – AMC Jan 07 '20 at 20:35