dict elements are sometimes treated as List and sometimes as String - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: dict elements are sometimes treated as List and sometimes as String (/thread-25405.html) |
dict elements are sometimes treated as List and sometimes as String - phython_mdr - Mar-29-2020 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 # 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. RE: dict elements are sometimes treated as List and sometimes as String - buran - Mar-29-2020 Please, post full traceback you get, in error tags. There is something else, because your code works: the second code works words = dict() token:str = "acto" words[token] = ["coat"] words[token].append("taco") print(words)
RE: dict elements are sometimes treated as List and sometimes as String - phython_mdr - Mar-30-2020 Hi Buran Thanks for helping. Here's the snippet and the error message words2 = dict() token: str ="acto" words2[token]="coat" words2[token].append("taco") #Line 16Traceback (most recent call last): File "/Users/Library/Preferences/PyCharmCE2019.3/scratches/QuickDictionary.py", line 16, in <module> words2[token].append("taco") AttributeError: 'str' object has no attribute 'append' ['coat', 'taco'] <re.Match object; span=(0, 0), match=''> Process finished with exit code 1 RE: dict elements are sometimes treated as List and sometimes as String - deanhystad - Mar-30-2020 If you want the dictionary to contain lists you need to add lists. "coat" is a string. ["coat"] is a list that contains a string. You can append to a list of strings. You cannot append to a string. Your first two examples run correctly because they initially add a list to the dictionary. The final example fails because it adds a word. I think you problem is a typo. RE: dict elements are sometimes treated as List and sometimes as String - phython_mdr - Apr-01-2020 (Mar-30-2020, 01:16 AM)deanhystad Wrote: If you want the dictionary to contain lists you need to add lists. "coat" is a string. ["coat"] is a list that contains a string. You can append to a list of strings. You cannot append to a string. Your first two examples run correctly because they initially add a list to the dictionary. The final example fails because it adds a word. I think you problem is a typo. Thanks for catching that - it did the trick. |