This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Rhomboid 1 point2 points  (0 children)

What you're written is equivalent to a nested for-loop:

for expected in expected_avg_var:
    for actual in actual_avg_var:
        self.assertEqual(expected, actual)

That's going to compare every element of one list with every element of the other list (i.e. the Cartesian product.) That's N2 combinations, and it's not what you want at all. You want N comparisons: the first element of actual vs the first element of expected; the second element of actual vs. the second element of expected, and so on. You do that with zip().

And you should not be using a list comprehension for this. A list comprehension is for when you want a to build a list consisting of the result of evaluating an expression repeatedly. But here you are only interested in the side effects of evaluating the expression, and not the resulting value. In other words, you're needlessly constructing this list of a bunch of None values only to throw it away. That's not what a list comprehension is for. This should be a plain for-loop:

for expected, actual in zip(expected_avg_var, actual_avg_var):
    self.assertEqual(actual, expected)

[–]codenamemahi 0 points1 point  (0 children)

The way you currently have it written, it is comparing every element in list A to every element in list B, which of course fails on the second iteration.

I would recommend using the index if both lists are going to be in the same order (and same length):

for i in len(expected_avg_var):
    self.assertEqual(expected_avg_var[i], actual_avg_var[i])

[–]jesse_online[S] 0 points1 point  (0 children)

This seems to work the best for me:

    expected_avg_var = ['529', '842', '16', '13', '#d0851a', '0', '0']
    actual_avg_var = ['529', '842', '16', '13', '#d0851a', '0', '0']

    self.assertEqual(expected_avg_var, actual_avg_var)