Step-by-step explanation of this short for loop, please

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-strings/lessons/string-methods/exercises/splitting-strings-ii

Hello,

I was wondering if some kind soul could explain me what exactly is happening, step-by-step or some similar kind of explanation, in the for loop in the following code:

authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni"

author_names = authors.split(',')

#print(author_names)

author_last_names = []
for name in author_names:
  author_last_names.append(name.split()[-1])
  
print(author_last_names)

After not succeeding in completing this exercise by myself I was forced to ask for a solution, but even then I was not able to fully understand this for loop.

Thank you very much.

1 Like

if that is what you are after, why not use tools which allow us to step through our code:

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

we loop over the author names, split these names in first and last name and then append the last name to a new list

2 Likes

Oh, I didn’t even know this beautiful thing existed. Thank you so much for the link provided sir, I’m definitely going to use it!

1 Like