I saw a post on LinkedIn. Someone was surprised by Python behavior. So was I. And maybe more will be.
It is easy to illustrate:
C:\Work\Python>type list1.py
lst = [ 'a', 'b', 'c', 'd' ]
print(lst)
lst.remove('b')
lst.remove('c')
print(lst)
C:\Work\Python>python list1.py
['a', 'b', 'c', 'd']
['a', 'd']
C:\Work\Python>type list2.py
lst = [ 'a', 'b', 'c', 'd' ]
print(lst)
for v in lst:
if v == 'b':
lst.remove(v)
if v == 'c':
lst.remove(v)
print(lst)
C:\Work\Python>python list2.py
['a', 'b', 'c', 'd']
['a', 'c', 'd']
C:\Work\Python>type List1.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class List1 {
public static void main(String[] args) {
List<String> lst = new ArrayList(Arrays.asList("a", "b", "c", "d"));
System.out.println(lst);
lst.remove("b");
lst.remove("c");
System.out.println(lst);
}
}
C:\Work\Python>javac List1.java
Note: List1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\Work\Python>java List1
[a, b, c, d]
[a, d]
C:\Work\Python>javac List2.java
C:\Work\Python>type List2.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class List2 {
public static void main(String[] args) {
List<String> lst = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
System.out.println(lst);
for(String v : lst) {
if(v.equals("b")) {
lst.remove(v);
}
if(v.equals("c")) {
lst.remove(v);
}
}
System.out.println(lst);
}
}
C:\Work\Python>java List2
[a, b, c, d]
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997)
at List2.main(List2.java:9)
C:\Work\Python>type List1.cs
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
IList<string> lst = new List<string> { "a", "b", "c", "d" };
Console.WriteLine(string.Join(",", lst.ToArray()));
lst.Remove("b");
lst.Remove("c");
Console.WriteLine(string.Join(",", lst.ToArray()));
}
}
C:\Work\Python>csc List1.cs
Microsoft (R) Visual C# Compiler version 4.12.0-3.24572.7 (dfa7fc6b)
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work\Python>List1
a,b,c,d
a,d
C:\Work\Python>type List2.cs
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
IList<string> lst = new List<string> { "a", "b", "c", "d" };
Console.WriteLine(string.Join(",", lst.ToArray()));
foreach(string v in lst)
{
if(v == "b")
{
lst.Remove(v);
}
if(v == "c")
{
lst.Remove(v);
}
}
Console.WriteLine(string.Join(",", lst.ToArray()));
}
}
C:\Work\Python>csc List2.cs
Microsoft (R) Visual C# Compiler version 4.12.0-3.24572.7 (dfa7fc6b)
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Work\Python>List2
a,b,c,d
Unhandled Exception: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at Program.Main(String[] args)
I like the Java and C# way better than the Python way.