Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree
Asher Orr
Python Development Techdegree Student 2,344 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
215,940 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
Asher Orr
Python Development Techdegree Student 2,344 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!