so when you use the a.clear()
ie removing all element from the list
then, list object become empty and where ever this object is used all of it's value become empty. this is why you are getting empty list of list in the final output.
what you need to go was, either use deepcopy()
or redefined the list object with reference to variable a
.
below code is what you are looking for for your solution
f = []
a = []
for i in range(10):
a.append(i) # use a.extend(i) if you want output [0,1,2,3,4,5,6,7,8,9]
f.append(a)
a = []
print(f)
# output [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
Below code is a little behind the scene case why your data which is removed from list a
is getting removed from f
list itself.
a = []
c = []
print('a->',id(a),'c->', id(c), )
# a-> 139917172826512 c-> 139917172828432
a.append(4)
print("after appending 4 to a , id(a)" ,id(a))
#after appending 4 to a , id(a) 139917172826512
c.append(a)
print("after appending a to c, id (C)-> ", id(c))
#after appending a to c, id (C)-> 139917172828432
print("id(c[0]) -> ", id(c[0]), "same as id of aa")
# id(c[0]) -> 139917172826512 same as id of aa
a.clear()
print("clear a = {}, c= {}, id(A) -> {}, id(c) -> {}, id(c[0]) -> {}".format(a,c,id(a), id(c), id(c[0])))
# clear a = [], c= [[]], id(A) -> 139917172826512, id(c) -> 139917172828432, id(c[0]) -> 139917172826512
a.append(4)
print("adding 4 again a = {}, c= {}, id(A) -> {}, id(c) -> {}, id(c[0]) -> {}".format(a,c,id(a), id(c), id(c[0])))
#adding 4 again a = [4], c= [[4]], id(A) -> 139917172826512, id(c) -> 139917172828432, id(c[0]) -> 139917172826512
a.pop()
print("removing last element from a again a = {}, c= {}, id(A) -> {}, id(c) -> {}, id(c[0]) -> {}".format(a,c,id(a), id(c), id(c[0])))
# removing last element from a again a = [], c= [[]], id(A) -> 139917172826512, id(c) -> 139917172828432, id(c[0]) -> 139917172826512
a.append(4)
print("adding 4 again a = {}, c= {}, id(A) -> {}, id(c) -> {}, id(c[0]) -> {}".format(a,c,id(a), id(c), id(c[0])))
# adding 4 again a = [4], c= [[4]], id(A) -> 139917172826512, id(c) -> 139917172828432, id(c[0]) -> 139917172826512
a = []
a.append(4)
print("changing the list object of a and intilising it again a = {}, c= {}, id(A) -> {}, id(c) -> {}, id(c[0]) -> {}".format(a,c,id(a), id(c), id(c[0])))
# changing the list object of a and intilising it again a = [4], c= [[4]], id(A) -> 139917172767552, id(c) -> 139917172828432, id(c[0]) -> 139917172826512
print("see how id associated with the c[0] changes")
# see how id associated with the c[0] changes