Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using nested lists/nested loops

I'm trying to figure out how to use nested lists/nested loops in Python. I'm supposed to write function(s) to create a new table that is comprised of adding the indices of 2 inputted tables in the form of lists. So, for example, if the function was addTables, I would run:

addTables([[1,1],[1,1]],[[2,2],[2,2]])

and I would get:

[[3,3],[3,3]]

I'm having trouble figuring it out. First, the function I wrote returns [3,3,3,3] when I run my code:

def addElements(element1,element2):
    newlist = []
    for i in range(0,len(element1)):
        for j in range(0,len(element1[i])):
            new_element = element1[i][j] + element2[i][j]
            newlist.append(new_element)
    return newlist

Second, we're supposed to use multiple functions. I can't figure out how to split up the program so that different parts are completed by different functions. Could someone point me in the right direction? Any tips would be greatly appreciated.

like image 692
Ismael Avatar asked Apr 16 '26 19:04

Ismael


2 Answers

First using nested for loops

def addTables(first, second):
    new_table = []
    for i in range(len(first)):
        new_list = []
        for j in range(len(first[0])):
            new_list.append(first[i][j] + second[i][j])
        new_table.append(new_list)
    return new_table

>>> addTables([[1,1],[1,1]],[[2,2],[2,2]])
[[3, 3], [3, 3]]

Another way to do this would be to break it up into two functions. The first can add two lists, then the outer function can call that to add tables.

def addLists(first, second):
    new_list = []
    for i in range(len(first)):
        new_list.append(first[i] + second[i])
    return new_list

def addTables(first, second):
    new_table = []
    for i in range(len(first)):
        new_table.append(addLists(first[i], second[i]))
    return new_table

>>> addTables([[1,1],[1,1]],[[2,2],[2,2]])
[[3, 3], [3, 3]]

More concisely, this can be done using nested list comprehensions

def addTables(first, second):
    return [[i+j for i,j in zip(a, b)] for a, b in zip(first, second)]

>>> addTables([[1,1],[1,1]],[[2,2],[2,2]])
[[3, 3], [3, 3]]

Lastly you can use numpy in the future, which will perform linear algebra very efficiently

import numpy as np

def addTables(first, second):
    return np.array(first) + np.array(second)

>>> addTables([[1,1],[1,1]],[[2,2],[2,2]])
array([[3, 3],
       [3, 3]])
like image 71
Cory Kramer Avatar answered Apr 19 '26 08:04

Cory Kramer


You're not initialising the inner lists. To do that, you need to append a new row at the start of the outer loop body. However, there is a much easier way. Behold the power of the standard library:

[[a+b for a,b in zip(arow, brow)] for arow, brow in zip(element1, element2)]

What zip does is to pair up the elements in two or more iterables, such as your lists. The other 'trick' is to use a list comprehension to build the new table.

like image 41
SwiftsNamesake Avatar answered Apr 19 '26 08:04

SwiftsNamesake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!