I am new to Python and just stumbled on adding list
to set
. Now I know the reason why this is not possible is that list
is mutable so its hashcode
(or the Python corresponding concept) will change. Reflecting on my programming in Java, I found that I have done this many times.
HashSet<ArrayList<String>> masterCollection = new HashSet<ArrayList<String>>();
ArrayList<String> a = new ArrayList<String>();
masterCollection.add(a);
a.add("Hello, World");
for(ArrayList<String> list : masterCollection) {
// do something to list
}
I did not notice any abnormal behaviors of the above code. I even used ArrayList
as keys to HashMap
and succeeded in accomplishing my task.
Therefore, I want to ask how internally Python is different in Java in this aspect and whether there are something I need to pay attention to when writing the above code.