Flawed 'for' command iteration

The following example seems to indicate that the integrity of “for” is questionable.
Please note that ‘c’ is not in ‘words’
#################################
phrase = “Don’t panic!”
plist = list(phrase)
words = ‘on tap’
for char in plist:
# if the char from plist is not in words, then remove it from plist
if char not in words:
plist.remove(char)
else:
pass

print(plist)
[‘o’, ‘n’, ‘t’, ’ ', ‘p’, ‘a’, ‘n’, ‘c’]

I understand what is happening - the initial state of ‘for’ is not preserved: the iteration is not consistent.

Maybe it’s me. However, same logic as implemented in tcl produces a result that reflects the intent of ‘foreach’ iteraction:
o n t ’ ’ p a n

Please advise on what I am missing about Python iteration.
Rick

A list iterator uses index-based iteration. Thus when your code removes the character at the current index, the next character in the list gets skipped. Consequently it never even tests whether “c” is in words. For example:

phrase = "Don't panic!"
plist = list(phrase)
words = 'on tap'
checked = []
for char in plist:
    checked.append(char)
    if char not in words:
        plist.remove(char)

>>> checked
['D', 'n', "'", ' ', 'p', 'a', 'n', 'i', '!']

You miss documentation.

Simplest way to get information is to use Python built-in help (Note: at the end describes situation you are in and provides one way how to address it):

>>> help('for')
The "for" statement
*******************

The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:

   for_stmt ::= "for" target_list "in" expression_list ":" suite
                ["else" ":" suite]

The expression list is evaluated once; it should yield an iterable
object.  An iterator is created for the result of the
"expression_list".  The suite is then executed once for each item
provided by the iterator, in the order returned by the iterator.  Each
item in turn is assigned to the target list using the standard rules
for assignments (see Assignment statements), and then the suite is
executed.  When the items are exhausted (which is immediately when the
sequence is empty or an iterator raises a "StopIteration" exception),
the suite in the "else" clause, if present, is executed, and the loop
terminates.

A "break" statement executed in the first suite terminates the loop
without executing the "else" clause’s suite.  A "continue" statement
executed in the first suite skips the rest of the suite and continues
with the next item, or with the "else" clause if there is no next
item.

The for-loop makes assignments to the variables(s) in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop:

   for i in range(10):
       print(i)
       i = 5             # this will not affect the for-loop
                         # because i will be overwritten with the next
                         # index in the range

Names in the target list are not deleted when the loop is finished,
but if the sequence is empty, they will not have been assigned to at
all by the loop.  Hint: the built-in function "range()" returns an
iterator of integers suitable to emulate the effect of Pascal’s "for i
:= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".

Note: There is a subtlety when the sequence is being modified by the
  loop (this can only occur for mutable sequences, e.g. lists).  An
  internal counter is used to keep track of which item is used next,
  and this is incremented on each iteration.  When this counter has
  reached the length of the sequence the loop terminates.  This means
  that if the suite deletes the current (or a previous) item from the
  sequence, the next item will be skipped (since it gets the index of
  the current item which has already been treated).  Likewise, if the
  suite inserts an item in the sequence before the current item, the
  current item will be treated again the next time through the loop.
  This can lead to nasty bugs that can be avoided by making a
  temporary copy using a slice of the whole sequence, e.g.,

     for x in a[:]:
         if x < 0: a.remove(x)

Related help topics: break, continue, while

First, thanks for the reply.

After some introspection and interactive debugging I deduced that the operation works as described. The post was to confirm that, indeed, my head was still screwed on somewhat correctly.

I had looked at the W3 docs for python instead of those at the org. That ain’t going to happen again.

However, I am a .net and tcl\tk developer and, for what it is worth, subtleties of this type are not well understood or expected particularly in the context of a programming language. Guess the head needs to be screwed back on, differently.

I want to like Python, but this is scary…

Thanks again,

Rick

Got it.

Thanks,

Rick

Creating a list just to prune it isn’t a common pattern in Python. It’s possible to do it reliably and efficiently, if you must. For example, plist[:] = (c for c in plist if c in words). This uses a generator expression, which avoids creating a temporary list of items to keep. The operation is performed in place. The kept values are shifted to the beginning of the list, and the rest of the list is discarded. But then, one may as well use a list comprehension the first time around, e.g. plist = [c for c in phrase if c in words].

Guess I will have to start thinking ‘python’. Thanks…