Search code examples
python-3.xnested-loopsnested-lists

Using for loops and enumerate to manipulate values within nested lists


I apologize if there is incorrect terminology through this post, I am relatively new to python and am trying something out of my wheel house.

I am trying to build a program that will add a value to the values within a nested list.

see nested list below.

List = [ nan, 4, 5, nan, nan, nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 5, nan, nan, nan, nan]

I would like the program to start at the second list and end before the final list in the sequence and also to skip the first and the last values within lists two through five. Essentially only adding values to the places where the 2 and the 3 are.

The solution should look like this:

List = [ nan, 4, 5, nan, nan, nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 3,  4,   5,  nan, nan], 
       [ nan, 4, 5, nan, nan, nan, nan]

I tried to tackle this as follows: Firstly, to create a 'for' loop range boundary condition that the program would iterate within, Since lists start at 0 and end at -1 I could add a range of (1,-2) and that would then exclude the first and last of the nested lists from the List. Next a nested 'for' loop that would, using enumerate, iterate the values within the individual nested list range I am interested in, the 2 and the 3 spot.

for index in range (1,-1):
   for index, item in enumerate(List, start = 1):     
      if item > 1:
         List[index] = round((List[index]+1),3)
print(List)

Enumerate function has a start function that if I set the 'if' statement to greater than 1 it would skip the nan values and start at the 2. I am unsure how to get the program to stop before the 5. Right now the program runs but none of the numbers change.

Any help would be appreciated.

Thank you.

EDIT: I have updated my example_list and code below:

example_list = [[ 0, 4, 5, 0, 0, 0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 2,  3,   5,  0, 0], 
[ 0, 4, 5, 0, 0, 0, 0]]

x = 1

for i, sub_list in enumerate(example_list[1:-1],start=1):
    for j, entry in enumerate(sub_list[1:-1][1:-1], start=2):
           if j > 1:
               example_list[j] = round((example_list[j]+    x),3)
           example_list[i][j] += 1
print(example_list)

I am running into a TypeError: can only concatenate list (not "int") to list error.


Solution

  • Your question is not completely well posed as already pointed out, but here are some ideas on how to solve your problem:

    If you have a nested list as

    example_list = [[ nan, 4, 5, nan, nan, nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 2,  3,   5,  nan, nan], 
       [ nan, 4, 5, nan, nan, nan, nan]
    ]
    

    then you can use the following snippet to

    start at the second list and end before the final list in the sequence and also to skip the first and the last values within lists two through five

    for i, sub_list in enumerate(example_list[1:-1],start=1):
        for j, entry in enumerate(sub_list[1:-1],start=1):
            # Perform an operation with that value, e.g. add 1
            example_list[i][j] += 1
    

    Essentially only adding values to the places where the 2 and the 3 are.

    Regarding the 2 and 3's in your list, they are only in columns 2 and 3, so you could use the following code snippet:

    for i, sub_list in enumerate(example_list[1:-1], start=1):
        for j, entry in enumerate(sub_list[2:4], start=2):
            # Perform an operation with that value, e.g. add 1
            example_list[i][j] += 1
    

    I hope this helps!