Home > Blockchain >  Iterating Over A List
Iterating Over A List

Time:12-01

I have a function that I'd like to iterate over a list of lists. The thing is, this list of lists is dynamic, meaning it changes based on user input.

Here is an example of my code:

result = func(5)
for z in Z[0]:
    result  = func(z)

where Z is the dynamic lists of lists and func is my function. The function should iterate over every element in the list subsetted list Z[0] and sum the results. The code works when the list of lists looks like this:

Z = [[1.1],[2.1],[3.1],[4.1]]

were Z[0] is just [1.1]. However sometimes this dynamic list of lists has more elements in each list, in which case i need to iterate over each element. Like below:

Z = [list([1.1, 1.3]),list([3.1]),list([4]])

in which case:

Z[0] = [list([1.1, 1.3])]

and my code should do the following:

result = func(5)
for z in Z[0]:
    result  = func(z)

....
func(1.1)   func(1.3)

When I try to run the code with the list of lists I get an error: "can only concatenate list (not "float") to list"

I'm sure it has something to do with trying to iterate over a list. If anyone has an easy fix to this, or knows the issue i'd love to know.

CodePudding user response:

its hard to kind of make sense of your question, but if you are just trying to sum each list of nums inside the main list then you can do something like

def sum_nums(nums: list):
    return [sum(num) for num in nums]


nums = [
    [1, 2],
    [7, 9, 10],
    [2],
    [9]
]

print(sum_nums(nums))

OUTPUT

[3, 26, 2, 9]

CodePudding user response:

If every element of Z is a list, just with differing sizes (i.e. [1.1], or [1.1, 2.2], etc.) You can iterate over the list and the size of the list will not matter.

for l in Z:
    result = 0

    for z in l:
        result  = func(z)

    # do something with inner list result

For each list in Z, [1.1] is a list, and then for each element in the lists in Z, if above then 1.1, run func on this value and then add to result.

If Z can be a list of elements that are not a list, (i.e. Z = [1.1, 2.2]) then this method will not work, but you have not shown this to be your case.

EDIT - added in result to list to get sums of inner lists individually

CodePudding user response:

Your input is:

Z = [[1.1, 1.3],[2.1],[3.1],[4.1]]

I think what you need to do is iterate over all elements, apply your function and add the results and return

def sum_of_all_elements(list_of_list):
   result = 0
   for nested_list in list_of_list:
      for element in nested_list:
         result  = func(element)
   return result
print(sum_of_all_elements(Z))

Using line comprehension

result = sum([ func(element) for element in nested_list for nested_list in Z])

CodePudding user response:

Your logic looks correct. I tested it on both examples you provided with the code below, and it produces the correct results.

def func(x):
    return x**2

Z1 = [[1.1],[2.1],[3.1],[4.1]] # works
Z2 = [list([1.1, 1.3]), list([3.1]), list([4])] # works
 
result1 = func(5)
for z in Z1[0]:
    result1  = func(z)
print(result1)
## Result is 26.21
# (5**2)   (1.1**2) = 25   1.21 = 26.21

result2 = func(5)
for z in Z2[0]:
    result2  = func(z)
print(result2)
## Result is 26.21
# (5**2)   (1.1**2)   (1.3**2) = 25   1.21   1.69 = 27.9

However, I believe your issue arises when the input array Z has more layers of nested lists than is expected. In the example below, the value "1" is formatted as a separate list. When the code iterates over 1.1 and 1.3, it can handle both of these cases because they are floats. Once the code gets to the third element which is formatted as a list [1], it is "surprised" and does not know how to handle the list because it was expecting a float. The code was not designed to expect this new extra list layer.

Z3 = [list([1.1, 1.3, [1]]), list([3.1]), list([4])] # does not work

result3 = func(5)
for z in Z3[0]:
    result3  = func(z) ## Error occurs on this line
print(result3)

To avoid this issue, you could make sure the user does not input more nested lists than is expected by your code. Alternatively, instead of using nested lists, you could use numpy arrays or pandas dataframes and add a new dimension instead of a new level of a nested list.

  • Related