Why the use of multiple for loops when += or extend() work (Task 18 - Thread Shed)

Link to the project: https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

The solution video shows the use of two for loops in order to get two layers deep into the list.

Code used inside the solution video:

thread_sold_split = []

for sale in thread_sold:
  for color in sale.split("&"):
    thread_sold_split.append(color)

My solution:

thread_sold_split = []

for color in thread_sold:
  thread_sold_split.extend(color.split("&")) 

or 

for color in thread_sold:
  thread_sold_split += color.split("&")

From my understanding of the differences between += and append is that if the expression on the right side of += is a list, it will add the whole value into the new variable instead of adding it individually. Using append will create a list of a list if the value is, for example, a list of string.

I don’t get why did the solution video shows that we need two for loops to do that when += or extend() works the same unless the value isn’t a list to begin with.

1 Like

In the beginning, it’s usually more instructive to go from the most explicit and straightforward and optimize after. The .split in your examples is clever but it hides what’s happening under the hood… (which is fine in an end-product).

I say in the beginning as opposed to for beginners… because a lot of elegant or more advanced concepts arise from the challenges met in the straightforward approach. It motivates and informs our methodology.

In many ways it’s like learning the value of money. Operations cost time and space. Sure, we have a method .sort() that automatically sorts our arrays, but if we never code and analyze different sorting algorithms, we don’t know the intrinsic cost of the .sort().

To this end, += for lists in python is considerably slower than .append() (because of the copying), and so when you scale a program up it is much worse.

I like the user Aaron Hall’s analysis of extend vs. append here: What is the difference between Python's list methods append and extend? - Stack Overflow

2 Likes

I assume one of the drawbacks of my examples of using split() is the execution time. However, Aaron Hall’s analysis of .extend and .append shows that appending multiple values using a loop command ran much slower than .extend. It is the other way around if we are only appending “one” element. In the case of this project, we are appending multiple values onto the list. I’ve also tested this using the same method that Aaron Hall’s used in his analysis except that I only added the in-place operator and the code relevant to this post.

This is the result:

>>> import timeit
>>>
>>> def append(thread_sold_split, thread_sold):
...     for sale in thread_sold:
...         for color in sale.split("&"):
...             thread_sold_split.append(color)
...
>>> def extend(thread_sold_split, thread_sold):
...     for color in thread_sold:
...         thread_sold_split.extend(color.split("&"))
...
>>> def iadd(thread_sold_split, thread_sold):
...     for color in thread_sold:
...         thread_sold_split += color.split("&")
...
>>> print(min(timeit.repeat(lambda: append([], ["white", "white&blue"]))))
0.46871879999999955
>>> print(min(timeit.repeat(lambda: extend([], ["white", "white&blue"]))))
0.38597000000000037
>>> print(min(timeit.repeat(lambda: iadd([], ["white", "white&blue"]))))
0.3738865000000011

These timings may vary but the timing of the in-place operator trumps .append and .extend.

Yes, I wasn’t critiquing the extend, just linking some more reading material for anybody interested.

But I think having too many fancy tools in the beginning is sometimes worse pedagogically. The curious and devoted students will always seek to find more satisfying answers.

1 Like

Of course, and thank you for linking the post here, it was something very interesting to read about, I also believe it’ll benefit students that are still learning the topic List much more because I saw a lot of posts asking about the differences of it, much more than the users searching a more concise solution of this project or asking questions related to it.

I agree, but as a student, when you write a code that has the same result as the solution, you’ll start to question your own solution, such as why is it different from the one in the exercise’s solution or is my code semantically correct. Then the student will come to the forum and post that to receive feedback about their solutions. I see it as an effective pedagogy approach for teaching more useful tools to beginners, I said “useful” because if the tools given to the beginner isn’t descriptive enough, they might use it just because it seems more concise but has knowledge of the tool’s functionality at all.

Anyway, I appreciate your feedback regarding my way of solving this task. I also went to learn about the lambda function, looks like a powerful function, still reading its functionality though.