Python altering list item in iteration
<p>I am trying to get this python code to get rid of punctuation marks associated with words and count the unique words. For some reason it's still counting both "hello." and "hello". Any help would be most appreciated. </p>

<pre><code>def word_distribution(words):
            word_dict = {}
            words = words.lower()
            words = words.split()
            for word in words:
                if ord('a') &lt;= ord(word[-1]) &lt;= ord('z'):
                    pass
                elif ord('A') &lt;= ord(word[-1]) &lt;= ord('Z'):
                    pass
                else: 
                    word[:-1]
            word_dict = {word:words.count(word)+1 for word in set(words)}
            return(word_dict)
</code></pre>

<p>There are certainly better way of achieving what you are trying to do but this answer fixes your code.</p>

<p>Strings are immutable and lists are mutable. Nowhere in your code you were modifying the list. and <code>words[-1]</code> wont have any impact because you were not re assigning it and string are immutable</p>

<pre><code>def word_distribution(words):
        word_dict = {}
        words = words.lower()
        words = words.split()
        for word in words:
            index = words.index(word)
            if ord('a') &lt;= ord(word[-1]) &lt;= ord('z'):
                pass
            elif ord('A') &lt;= ord(word[-1]) &lt;= ord('Z'):
                pass
            else: 
                word = word[:-1]
                words[index] = word 

        word_dict = {word:words.count(word) for word in set(words)}
        return(word_dict)
</code></pre>

python