In Java, I can do it using Iterator and then using .remove() to remove the last element returned by the iterator, for example:
import java.util.*; public class ConcurrentMod { public static void main(String[] args) { List<String> colors = new ArrayList<String>(Arrays.asList("red", "green", "blue", "purple")); for (Iterator<String> it = colors.iterator(); it.hasNext(); ) { String color = it.next(); System.out.println(color); if (color.equals("green")) it.remove(); } System.out.println("At the end, colors = " + colors); } }
How do I do this in Python? I cannot change the list while I iterate over it in a for loop, because it makes the material skip (see here ). And there seems to be no equivalent to the Iterator Java interface.
python iterator list loops
user102008 Aug 30 '09 at 2:18 2009-08-30 02:18
source share