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

Passed the section of the challenge but when running in a terminal it prints out individual letters rather than courses

'''teachers = {"eboni williams": "jenkins", "jillian mele": "python dev", "carley shimkus": "ruby dev", "hillary vaughn": "artificial intelligence", "lisa booth": "machine learning", "jedediah bilah": "jenkins", "emelie ikeda": "jenkins"}'''

''' def courses(teachers): course_list = [] for value in teachers.values(): course_list += value return course_list

print (courses(teachers)) '''

Returns

'''['a', 'r', 't', 'i', 'f', 'i', 'c', 'i', 'a', 'l', ' ', 'i', 'n', 't', 'e', 'l', 'l', 'i', 'g', 'e', 'n', 'c', 'e', 'j', 'e', 'n', 'k', 'i', 'n', 's', 'm', 'a', 'c', 'h', 'i', 'n', 'e', ' ', 'l', 'e', 'a', 'r', 'n', 'i', 'n', 'g', 'j', 'e', 'n', 'k', 'i', 'n', 's', 'p', 'y', 't', 'h', 'o', 'n', ' ', 'd', 'e', 'v', 'j', 'e', 'n', 'k', 'i', 'n', 's', 'r', 'u', 'b', 'y', ' ', 'd', 'e', 'v']'''

1 Answer

Ben Hedgepeth
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Hedgepeth
Python Web Development Techdegree Student 10,287 Points

You are doing augmented assignment using two different types, list and str. What you are doing is this:

items = []

value = 'hello'

items = items + value # [] + ['h', 'e', 'l', 'l', 'o']

# items is now ['h', 'e', 'l', 'l', 'o']

In Python, a string is an immutable sequence. A list is also a sequence. In order to perform += operation, Python performs a coercion of sorts by spliting the string into a list of characters and extending the list to which you're attempting to add elements to. Instead of doing augmented addition, I would suggest using .append() within your for loop.