2

I am using a 'for loop' to eliminate the item step by step and generate a new list(feature_combination) including different combinations.

feature_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] feature_combination = [] for i in range(7): feature_list.pop() feature_combination.append(feature_list) feature_combination 

The ideal output should be:

[['A', 'B', 'C', 'D', 'E', 'F'],['A', 'B', 'C', 'D', 'E'],['A', 'B', 'C', 'D'],['A', 'B', 'C'],['A', 'B'],['A'], []] 

But the current output is: [[], [], [], [], [], [], []]

When I print the progress step by step:

feature_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] feature_combination = [] for i in range(7): feature_list.pop() print(feature_list) 

I can get the following the results:

['A', 'B', 'C', 'D', 'E', 'F'] ['A', 'B', 'C', 'D', 'E'] ['A', 'B', 'C', 'D'] ['A', 'B', 'C'] ['A', 'B'] ['A'] [] 

So, why I cannot append these results to an empty list? What is the problem?

3 Answers 3

5

It's because when you call feature_combination.append(feature_list), you are appending a reference to feature_list, not the actual value of feature_list. Since feature_list is empty at the end of the for loop, all of the references to it are empty as well.

You can fix it by changing feature_combination.append(feature_list) to feature_combination.append(feature_list.copy()), which makes a copy of the list to store.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. To the OP, if you want a bit more detail on what is going on, this section of the Python FAQ might be useful - docs.python.org/3/faq/…
1

First of all, you need to pass an index into pop in order to specify which element to delete. Though I find this unesaccary, instead you could use slicing. Below is an example of how you could accomplish your goal. This code adjusts to your desired output.

feature_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] feature_combination = [] for i in range(7): feature_list = feature_list[:-1] feature_combination.append(feature_list) print(feature_combination) 

output

[['A', 'B', 'C', 'D', 'E', 'F'], ['A', 'B', 'C', 'D', 'E'], ['A', 'B', 'C', 'D'], ['A', 'B', 'C'], ['A', 'B'], ['A'], []] 

Comments

1

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'], []] 

2 Comments

Thanks for your explanation, very detailed and helpful!
glad to help anytime

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.