Python 3: Thread Shed Excercise

Hello, I have a question regarding the following exercise:
https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

I am aware that a similar discussion was posted by @lucylee3476390680 related to question 8 of this exercise. However, I would like to take this discussion one step further.

In Question 8 of the Thread Shed exercise we were expected to construct two for-loops to iterate through items in a list. The goal was to clean the whitespace off the items and return a list with the same format and without unnecessary white space surrounding the items.

The Code:

dirty_list = [['A   ', 'B   '], [ '   C ', ' D   ']]

cleaned_list = []
for groups in dirty_list:
    cleaned_items = []
    for item in groups:
        cleaned_items.append(item.strip())
        cleaned_list.append(cleaned_items)

print(cleaned_list)

The Result:

[['A', 'B'], ['A', 'B'], ['C', 'D'], ['C', 'D']]

The Breakdown:
In the code above, an empty list called cleanded_list = [] was created to store items and their corresponding groups once the cleaning operation was performed.

Two for-loops have been used:

The first was used to gain access to the sub-lists “groups” (of the dirty list). Another empty list called cleaned_items = [] was then created to store items that would return from the second for-loop.

The second for-loop was used to gain access to the “items” ((A, B) and (C, D) )within the group lists. After this, the items would be stripped of unnecessary whitespace and would be appended to the cleaned_items list. Thereafter, the cleaned_items lists would be appended within the cleaned_list.

When printing cleaned_lists, one would expect to see the following:

[['A',  'B'], ['C',  'D']]

However, this is not the case. Instead we get the following result:

[['A', 'B'], ['A', 'B'], ['C', 'D'], ['C', 'D']]

How can we avoid the doubleing which is taking place above?

Thank you for your support.

1 Like

It is because you have defined the cleaned list twice, both outside of the for loop and inside the first for loop. When running the second for loop it appends the same items to the same list twice because it iterates over the same empty list twice.

The two for loops are used to unpack the dirty_list.
For example, groups[0] of dirty_list is['A ', 'B ']. Item[0] of groups[0] would be 'A '.

cleaned_list is only needs to be defined globally outside of the for loops one time as seen below.

dirty_list = [['A ', 'B '], [ ’ C ', ’ D ']]

cleaned_list =
for groups in dirty_list:
for item in groups:
cleaned_list.append(item.strip())

Try this code and it should work! @system1063766339 @lucylee3476390680

Hello all, please disregard the solution proposed by @psweaty. Unfortunately, if we follow the recommended step to remove the empty list cleaned_items = [] which is defined within the first for-loop we will lose the grouping structure which we are trying to maintain.

Furthermore, if we follow @psweaty steps directly we are not surprisingly presented with a NameError as the list cleaned_items list is no longer definable.

@psweaty Code:

dirty_list = [['A ', 'B    '], [' C ', ' D    ']]

cleaned_list = []
for groups in dirty_list:
    for item in groups:
        cleaned_items.append(item.strip())
        cleaned_list.append(cleaned_items)
        
print(cleaned_list)

@psweaty Result:

Traceback (most recent call last):
  File "...", line 6, in <module>
    cleaned_items.append(item.strip())
NameError: name 'cleaned_items' is not defined
>>> 
1 Like

Hey bud, you asked why you were getting repeat items in your list. You have multiple errors in your code. I fixed your first problem. Here is the solution to your second problem.

You just need one list and then you just have to strip each item and append it to that one list. The only list you need is cleaned_list. You then do cleaned_list.append(item.strip()). It is illogical to append a list to another list. I even coded it myself this time and made sure it worked.

dirty_list = [['A ', 'B ‘], [’ C ', ’ D ']]

cleaned_list =
for groups in dirty_list:
for item in groups:
cleaned_list.append(item.strip())

print(cleaned_list)

Hello all, once again please disregard the solution proposed by @psweaty. If we follow the steps provided we lose the grouping structure we are trying to maintain.

@psweaty Code:

dirty_list = [['A    ', 'B     '], ['    C ', ' D ']]

cleaned_list = []
for groups in dirty_list:
    for item in groups:
        cleaned_list.append(item.strip())

print(cleaned_list)

@psweaty Result:

['A', 'B', 'C', 'D']

This result does not allign with our derised result: [[‘A’, ‘B’], [‘C’, ‘D’].

I have managed to source the problem and am working on a video tutorial for anyone who would like to see the solution.

1 Like

Here is a video providing an overview of the problem. The fixed code can also be found below. Moral of the story “watch your whitespace!” :sweat_smile:

Click Here for Video Solution

The Fixed Code:

dirty_list = [['A   ', 'B   '], [ '   C ', ' D   ']]

cleaned_list = []
for groups in dirty_list:
    cleaned_items = []
    for item in groups:
        cleaned_items.append(item.strip())
    cleaned_list.append(cleaned_items)

print(cleaned_list)

The Result:

[['A', 'B'], ['C', 'D']]

All the best! :yum:

1 Like

Great work! This was really helpful. Thanks, I learned a lot

1 Like