cycle with possibility to remove elements
<p>I'm trying to write a class which works in the same way as the cycle from itertools module but has some additional functionality. I want to have possibility to remove elements from a list along which I'm iterating.</p>

<p>Here is my code:</p>

<pre><code>class myCycle(object):
    def __init__(self, list):
        self.list = list

    def __iter__(self):
        def iter(self):
            while True:
                if not self.list:
                    break
                for e in self.list:
                    yield e
        return iter(self)

    def remove(self, e):
        self.list.remove(e)
</code></pre>

<p>It works fine with one exception. Let's look at example:</p>

<pre><code>&gt;&gt;&gt; for i in c:
...     print i
...     if i == 1:
...         c.remove(1)
...     if i == 3:
...         break
... 
1
3
&gt;&gt;&gt; 
</code></pre>

<p>After removing 1 from the list, index move and that is probably reason why I don't have 2 in my output.</p>

<p>But how can I get it working? Or maybe there is some other way to get this functionality?</p>

<p><strong>EDIT:</strong></p>

<p>After your comments I will try to explain what I'm trying to do.</p>

<p>I have a string with player actions which can look like this one:</p>

<pre><code>actions = "sBcffcffcfrfccc"
</code></pre>

<p>It's from poker, action f mean fold, c call and so one. Most interesting for us is fold. </p>

<p>And I also have list of players:</p>

<pre><code>players = ['Saxum', 'Erasmus', 'Anders', 'Ginger', 'Adam',
           'Crusoe', 'OgoPogo', 'Hari', 'Sanja', 'Hooke']
</code></pre>

<p>And I want to assign action to each player. So we are going through actions and players:</p>

<pre><code>Saxum -&gt; s
Erasmus -&gt; B
Anders -&gt; c
Ginger -&gt; f

Ginger folded, we should remove this player. So how player list look like now:

    players = ['Saxum', 'Erasmus', 'Anders', 'Adam',
               'Crusoe', 'OgoPogo', 'Hari', 'Sanja', 'Hooke']

Adam -&gt; f

Adam folded, we should remove this player. So how player list look like now:

    players = ['Saxum', 'Erasmus', 'Anders', 
               'Crusoe', 'OgoPogo', 'Hari', 'Sanja', 'Hooke']

Crusoe -&gt; c
OgoPogo -&gt; f

OgoPogo folded, we should remove this player. So how player list look like now:

    players = ['Saxum', 'Erasmus', 'Anders', 
               'Crusoe', 'Hari', 'Sanja', 'Hooke']

Hari -&gt; f

Hari folded, we should remove this player. So how player list look like now:

    players = ['Saxum', 'Erasmus', 'Anders', 
               'Crusoe', 'Sanja', 'Hooke']

Sanja -&gt; c
Hooke -&gt; f

Hooke folded, we should remove this player. So how player list look like now:

    players = ['Saxum', 'Erasmus', 'Anders', 
               'Crusoe', 'Sanja']

Hooke was last on the list, so we start from beginning.

Saxum -&gt; r
Erasmus -&gt; f

Erasmus folded, we should remove this player. So how player list look like now:

    players = ['Saxum', 'Anders', 
           'Crusoe', 'Sanja']

Anders -&gt; c
Crusoe -&gt; c
Sanja -&gt; c
</code></pre>

<p>And that was reason why I started to implement myCycle. But maybe there is better way to do this?</p>
