Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

In a print statement with a "sep" parameter, the values are not being separated. Why is this happening?

Hi everyone! I have a list (panthers_players) containing dictionaries.

[{'name': 'Phillip Helm', 'guardians': ['Thomas', 'Helm', 'and', 'Eva', 'Jones'], 'experience': True, 'height': 44}, {'name': 'Suzane Greenberg', 'guardians': ['Henrietta', 'Dumas'], 'experience': True, 'height': 44}, {'name': 'Joe Smith', 'guardians': ['Jim', 'Smith', 'and', 'Jan', 'Smith'], 'experience': True, 'height': 42}, {'name': 'Joe Kavalier', 'guardians': ['Sam', 'Kavalier', 'and', 'Elaine', 'Kavalier'], 'experience': False, 'height': 39}, {'name': 'Sammy Adams', 'guardians': ['Jeff', 'Adams', 'and', 'Gary', 'Adams'], 'experience': False, 'height': 45}, {'name': 'Matt Gill', 'guardians': ['Charles', 'Gill', 'and', 'Sylvia', 'Gill'], 'experience': False, 'height': 40}]

I'm trying to print the guardians on one line, separated by commas. Like this:

Thomas Helm and Eva Jones, Henrietta Dumas, Jim Smith and Jan Smith, Sam Kavalier and Elaine Kavalier, Jeff Adams and Gary Adams, Charles Gill and Sylvia Gill

I tried this code:

panthers_players_guardians = []
                for player in panthers_players:
                    panthers_players_guardians.append(player["guardians"])
                for entry in panthers_players_guardians:
                    guardians_string = " ".join(entry)
                    print(guardians_string, end = " ", sep = ", ")

However, no commas separate the values. The console prints:

Thomas Helm and Eva Jones Henrietta Dumas Jim Smith and Jan Smith Sam Kavalier and Elaine Kavalier Jeff Adams and Gary Adams Charles Gill and Sylvia Gill

If I understand correctly, I thought my code would do this:

panthers_players_guardians = []
#create new list, panthers_players_guardians
                for player in panthers_players:
#loop through each dictionary in panthers_players
                    panthers_players_guardians.append(player["guardians"])
#add the value for "guardians" to the list panthers_players_guardians.
                for entry in panthers_players_guardians:
#loop through each guardian list in panthers_players_guardian
                    guardians_string = " ".join(entry)
#adds each guardian in panthers_players_guardian to a string called guardians_string
                    print(guardians_string, end = " ", sep = ", ")
#prints guardians_string. Each value has a space added to the end of it, and will then be separated by a comma.

This is obviously not the case, though. Any ideas on why there are no commas separating each value?

I'm also open to any differing strategies altogether!

Thank you for your time!

Note: For this assignment, I have to:

  • Keep the guardians in panthers_players as a list.
  • print the guardians as a comma-separated string.

1 Answer

Steven Parker
Steven Parker
231,007 Points

Since there's only one thing being printed at a time (guardians_string), the separator isn't used. It wouldn't help to print the unjoined string at this point as separate items, since it would add commas between every name.

But instead of printing within the loop, you could create a new list of the joined names and then print that out as separate items when the loop is done:

guardians_list = []                          # new: create a new list
for entry in panthers_players_guardians:
    guardians_string = " ".join(entry)
    guardians_list.append(guardians_string)  # new: add the name string to the new list
print(*guardians_list, sep = ", ")           # new: NOW print the big list with the separators

This might be another good place for a list comprehension. You could replace all that with this:

guardians_list = [" ".join(entry) for entry in panthers_players_guardians]
print(*guardians_list, sep = ", ")
Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,408 Points

Thank you, Steven!

I appreciate you explaining why my initial code did not work (and suggesting both solutions, which work.) Your posts are always well-written and easy to understand. Thank you again!