List created from a while loop throws very long and strange error

In the exercise on Task 4 I first created the variable with a for loop successfully. The code ran without an error. I then decided to practice creating the variable with a list comprehension, and that too ran as expected and no error was thrown. I then wanted to practice creating the variable with a while loop and that also was successful, but after the list of 12 sample numbers was randomly created, my next line of code (plt.plot(numbers_a, numbers_b)) throws the following error.
Traceback (most recent call last): File "", line 17, in <module> plt.plot(numbers_a, numbers_b) File "/usr/local/lib/python3.6/dist-packages/matplotlib/", line 3317, in plot ret = ax.plot(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/matplotlib/", line 1898, in inner return func(ax, *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/", line 1406, in plot for line in self._get_lines(*args, **kwargs): File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/", line 407, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/", line 385, in _plot_args x, y = self._xy_from_xy(x, y) File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/", line 244, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (12,) and (13,).

Why does plt.plot(numbers_a, numbers_b) throw this error for a list created from a while loop, but not for a list created by a for loop or list comprehension?

It’s part of the traceback and it’s well worth learning how they’re set up as they’re packed with useful information. In this case though all you really need to focus on is the last line. Note that this would be the most recent operation/function call (the order is backwards as mentioned by most recent call last).

The last line is:

ValueError: x and y must have same first dimension, but have shapes (12,) and (13,)

Chances are your loops aren’t equivalent in each case so make sure the values you’re passing are the same size (if in doubt, print and check their size).

1 Like

I see where I made my mistake. Thanks for the clarification

1 Like