FAQ: String Methods - Review

This community-built FAQ covers the “Review” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

11 posts were split to a new topic: F-string works now!

3 posts were split to a new topic: How can we reference the right index if all our lists are seperate?

2 posts were merged into an existing topic: How can we reference the right index if all our lists are seperate?

5 posts were merged into an existing topic: How can we reference the right index if all our lists are seperate?

2 posts were split to a new topic: How does only selecting index 0,1,2 get all the poem details?

5 posts were split to a new topic: What if I use += instead of append?

2 posts were merged into an existing topic: How can we reference the right index if all our lists are seperate?

This is regarding the project Thread Shed

https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

For step number 6
( Now, iterate through daily_transactions (remember, this is a list of strings currently), and for each transaction, split the string around whatever character you replaced the ;,; artifacts with in Step 2 .
Append each of these split strings (which are lists now) to our new list daily_transactions_split .)

I wrote my code as below:

for i in range(len(daily_transactions)):
  trans_splitted = daily_transactions[i].split(":")
  daily_transaction_split.append(trans_splitted)
print(daily_transaction_split)

And the youtube video wrote the code like this:

for i in daily_transactions:
    split = i.split(":")
    daily_transaction_split.append(split)
print(daily_transaction_split)

Did I do it wrong? Why is the youtube video not using index as a reference and instead just uses for ___ in ___ instead of using for ___ in range(len(___))?

The method used is for read only, which is all we need. We are not modifying the read list, so don’t need the indices.

Hi mtf, thanks for taking the time to help respond.

Could you help explain what you mean by “read only” as well as “we are not modifying the read list, so we don’t need the indices”?

I thought we are modifying the list by splitting it based on “:”

:face_with_monocle:

1 Like

We are not splitting the input list, only the elements it contains, with the result going to another data structure. We only iterate over and read (poll) the elements of the input list. for..in doesn’t skip any elements (it iterates the entire list) so no values will be missed.

for..in cannot be used if we are planning to alter the input list as we iterate it. To do that we need the indices so we write the change to the correct element.

1 Like

I think I understand. I confuse the input list and the elements inside quite often. Thanks for explaining.

1 Like

You’re welcome. Keep in mind that we can always print intermediate results at various stages to confirm our expectations.

Yes I usually always print after every question. I also use:

http://www.pythontutor.com/visualize.html#mode=display

To try to understand what it is saying. I think I just need more practice with for statements to understand when it is appropriate to go through the indices of a list and when it is appropriate to go through the elements of a list.

We usually only need the indices if we plan to modify an element during iteration. The index gives us direct access so that we can refer to the exact element when performing the change.

>>> a = [1, 2, 3, 5, 8, 12, 15, 18, 21, 25]
>>> for x in range(len(a)):
	print (a[x] if a[x] % 5 == 0 else None)

	
None
None
None
5
None
None
15
None
None
25
>>> 

The above could be done with a read only loop since we aren’t mutating the list.

>>> a = [1, 2, 3, 5, 8, 12, 15, 18, 21, 25]
>>> for x in a:
	print (x if x % 5 == 0 else None)

	
None
None
None
5
None
None
15
None
None
25
>>> 

Let’s mess around with this…

>>> a = [1, 2, 3, 6, 8, 11, 15, 16, 21, 25]
>>> for x in range(len(a)):
	a[x] += 4
	print (a[x] if a[x] % 5 == 0 else None)

	
5
None
None
10
None
15
None
20
25
None
>>> a
[5, 6, 7, 10, 12, 15, 19, 20, 25, 29]
>>> 

Notice that we mutated the original list?

1 Like

Oh my, yes that was an easy to understand example. Thank you very much.

I came across another part of the project that I was having some trouble understanding:

This was given by the youtube video. The list looked like this:

['white', 'white&blue', 'white&blue', 'white', 'white&yellow', 'purple', 'purple&yellow', 'purple&yellow', 'blue', 'blue', 'purple&blue', 'white', 'white&red', 'white&blue&red', 'blue', 'green&blue', 'green&blue&red', 'green&blue&red', 'black', 'black&yellow', 'white&black&yellow'...]
for thread in thread_sold:
  for color in thread.split("&"):
    thread_sold_split.append(color)

and this was my attempt:

for thread in thread_sold:
  if thread.find("&") == -1:
    thread_sold_split.append(thread)
  elif thread.find("&") > 0:
    split_thread = thread.split("&")
    thread_sold_split.append(split_thread)

I understand that the output of mine created a list within a list (but I’m not entirely sure why)

And I was trying to understand the solution given:

but when it said:
for thread in thread_sold:
for color in thread.split("&"):
thread_sold_split.append(color) <----------

I thought this would return the letter of the words like “w” “h” “i” “t” “e” because it was looping twice.

1 Like

That gives pause. Times like this we might be better to tear things down to a point where we have a clear foundation. We are, after all the ones who make this happen.

How evident is the data environment? That is, what do we start with, what to we produce from it, and what is the state of things when all is said and done?

for thread in thread_sold:
  if thread.find("&") == -1:
    thread_sold_split.append(thread)
  elif thread.find("&") > 0:
    split_thread = thread.split("&")
    thread_sold_split.append(split_thread)

to my understanding:
for the first “if” if we find & and it is -1 (which means there is no &)
let’s append this color to the list
for the second “if” if we find & and it is > 0 (which means there is an &)
first split the & from the color
and then append it to the list

So I’m assuming that [“blue&white”] --> will get & removed, then gets appended to the list as [… [blue, white] …]

I’m not sure if I am reading it correctly but that is to my knowledge

for the second code:

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

for the first element (whether it is blue or blue&white):
for the letter in the first element (“w” “h” … “&”) we split out “&”
then we append “color” to the list

But isn’t “color” here represented as a letter like “w” or “h”? We split out the & symbol so it is no longer there but we append the individual letters to the list (which was my assumption).

But what happened was it appended the whole words “blue” “white” etc to the list instead. I am a bit confused because I thought “thread” here would indicate whole words while “color” indicates letters in the words.

Not to derail this approach… split() only occurs on the separator we supply. We don’t need to test for it beforehand.

>>> 'word'.split('&')
['word']
>>> 'word&word'.split('&')
['word', 'word']
>>> 

In both cases we are given back a list object. It’s now up to us how to disseminate it.