I was a little confused by adding an integer to an empty list in Python. My goal is to add an integer(eg:5) to an empty list inside of an empty list. For example:
tempList = [[], [], [], [], [5], [], [], [], []]
I found that if I created the empty list with these code, the result was not what I was looking for:
In[1]: tempList = [[]]*9
In[2]: tempList
Out[1]: [[], [], [], [], [], [], [], [], []]
In[3]: tempList[4].append(5)
In[4]: tempList
Out[2]: [[5], [5], [5], [5], [5], [5], [5], [5], [5]]
However if I created the empty list by simply typing it, and didn't change any other code, the final result is different and it became what I was looking for:
In[5]: tempList = [[],[],[],[],[],[],[],[],[]]
In[6]: tempList[4].append(5)
In[7]: tempList
Out[3]: [[], [], [], [], [5], [], [], [], []]
The help I need are: 1, What causes this? 2, For line "In[5]: tempList = [[],[],[],[],[],[],[],[],[]]", is there a simple way to write it if the empty list has length of 100?
Thank you!