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 trialStefan Borghesani
1,402 PointsDifferent way to accomplish the show_list function?
This is how I handled the show_list function:
def show_list():
show_list = "\n".join(shopping_list)
print("Here is your list: \n{}".format(show_list))
This seems to work exactly the same way. Is there any benefit to using a for loop as opposed to just printing the list with some formatting?
2 Answers
Charlie Bird
7,277 PointsIn this case the result is the same, it's up to you how you choose to format your output.
The use of a for
loop may help in other instances where you want something to happen for each item in the list, e.g. you might want to add the items to another list or add up prices.
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 PointsI did something similar:
def show_list(list): lista = ", ".join(list) + "." print("Your shopping list: {}".format(lista))