A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object. refer this.
This is because the feature_list points to a specific object, which keeps updating as you pop are subsequently. You are basically creating a list that contains [object, object, object ...] all pointing to the same feature_list object. As you keep popping and updating the object, the list that collects multiple instances of this same object also gets updated with this object.
Here is how you can test this happening -
feature_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] feature_combination = [] for i in range(7): feature_list.pop() feature_combination.append(feature_list) print('iteration', i) print(feature_combination) #Print the primary list after each iteration
iteration 0 [['A', 'B', 'C', 'D', 'E', 'F']] iteration 1 [['A', 'B', 'C', 'D', 'E'], ['A', 'B', 'C', 'D', 'E']] iteration 2 [['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'D']] iteration 3 [['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C'], ['A', 'B', 'C']] iteration 4 [['A', 'B'], ['A', 'B'], ['A', 'B'], ['A', 'B'], ['A', 'B']] iteration 5 [['A'], ['A'], ['A'], ['A'], ['A'], ['A']] iteration 6 [[], [], [], [], [], [], []]`
Notice, that after each iteration, every instance of the sublist is being updated after the pop and reflect inside the main list.
A fix
A fix is to use a slice to get and store a copy.
feature_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] feature_combination = [] for i in range(7): feature_list.pop() print(feature_list) feature_combination.append(feature_list[:]) #<---- feature_combination
[['A', 'B', 'C', 'D', 'E', 'F'], ['A', 'B', 'C', 'D', 'E'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C'], ['A', 'B'], ['A'], []]