Mar-29-2020, 05:06 PM
(This post was last modified: Mar-29-2020, 05:07 PM by phython_mdr.)
I'm just learning Python by developing an anagram finder.
After a bit of debugging it seems that if I check "if token not in words" then the words dictionary treats its elements as List items.
But if I assign a value without doing the "if" check, the values are treated as Strings (rather than a List of strings).
Is there somewhere I can research why the behavior changes? Is there a way to force the dictionary to make the values Lists?
I'm using PyCharm 2019.3.4 for Mac with Python 3.7.2
After a bit of debugging it seems that if I check "if token not in words" then the words dictionary treats its elements as List items.
But if I assign a value without doing the "if" check, the values are treated as Strings (rather than a List of strings).
Is there somewhere I can research why the behavior changes? Is there a way to force the dictionary to make the values Lists?
I'm using PyCharm 2019.3.4 for Mac with Python 3.7.2
# This code block treats words[] values as List items, allowing append to work words = dict() token: str ="acto" if token not in words: words[token] = ["coat"] else: words[token].append("coat") words[token].append("taco") #In this code block, "taco" is appended to the List associated with the "acto" key -> ['coat', 'taco'] for x in words: print (words[x])
# This code block treats words[] values as Strings items. Append does not work words = dict() token: str ="acto" words[token] = ["coat"] words[token].append("taco") #blows up because String has no append method.