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 Python Collections (Retired) Dictionaries Word Count

I see this syntax popping up here and there, though it hasn't been covered yet. Is it something that will be taught? or

absorbed through osmosis or something.

# this line
dic[word] =  dic[word] + 1 if word in dic else 1
# in this code
def word_count(string):
  words = string.split(' ')
  dic = dict('')
  for word in words:
     dic[word] =  dic[word] + 1 if word in dic else 1
  return dic

1 Answer

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Are you unsure as to the line as a whole, or elements within it?

Perhaps the line written as follows makes it clearer

for word in words:
    if word in dic:
        dic[word] = dic[word] + 1
    else:
        dic[word] = 1

You will be taught control flow as you move through the lessons, using keywords to loop over things, terminate loops, and create conditional branches through code.

happy to explain the individual parts of the code if you need it

James

right, that's how it has been taught so far. but sometimes i see it the other way. i recognize it all on one line pretty much, it's just unfamiliar