Python, not able to append to a list from a recursive function
Question: I am in the mid-way of writing a code to find all possible solutions of a
input similar like `"a&b|c!d|a"`, where a,b,c,d all are booleans and &-`and`,
|-`or` !-`not` are the operators. By solution I mean the set of values of
these variables which makes the input expression give `True`.

I am able to print all possible combinations of the variables, but I am not
able to retain them (in this case, in a list) for later use.

What's wrong in the way I am doing it? Are there better ways to store them?
`generate_combination` is the method in which I am trying to do this.

**Code:**

    
    
    import operator
    
    # global all_combinations
    all_combinations=[]
    answers=[]
    
    def solve(combination, input, rank):
        try:
            substituted_str=""
            for i in input:
                if i in combination:
                    substituted_str+=combination[i]
                else:
                    substituted_str+=i
            print substituted_str
            # for item in rank:
        except:
            pass
    
    def generate_combination(variables,comb_dict, length, current_index):
        if len(comb_dict)==length:
            print comb_dict #Each combination , coming out right
            all_combinations.append(comb_dict)
            print all_combinations,"\n" #This is not working as expected
        else:
            for i in [1,0]:
                comb_dict[variables[current_index]]=i
                generate_combination(variables,comb_dict, length,current_index+1)
                comb_dict.pop(variables[current_index], None)
    
    
    def main(input,variables,order):
        rank=sorted(order.items(), key=operator.itemgetter(1))
    
        generate_combination(variables, {}, len(variables), 0)
    
        for combination in all_combinations:
            print combination
            ans=solve(combination, input, rank)
            ans=[]
            answers.extend(ans)
    
        # for answer in answers:
        #     print answer
    
    def nothing():
        pass
    
    if __name__ == '__main__':
        # print "Enter your symbols for :\n"
        # And=raw_input("And = ")
        # Or=raw_input("Or = ")
        # Not=raw_input("Not = ")
        # input_str=raw_input("Enter the expression :")
    
        And,Or,Not,input_str='&','|','!','a&b|c!d|a'
        input_str=input_str.replace(" ","")
    
    
        mapping={And:"&", Or:"|", Not:"!"}
        order={"&":3, "|":2, "!":1}
    
    
        variables=[]
        processed_str=""
    
        for i in input_str:
            if i in mapping:
                processed_str+=mapping[i]
            else:
                processed_str+=i
                variables.append(i)
        variables=list(set(variables))
    
        print "Reconstituted string : ",processed_str
        print "Variables : ",variables,"\n"
    
        main(processed_str,variables,order)
    

**Current Output:**

    
    
    Reconstituted string :  a&b|c!d|a
    Variables :  ['a', 'c', 'b', 'd'] 
    
    {'a': 1, 'c': 1, 'b': 1, 'd': 1}
    [{'a': 1, 'c': 1, 'b': 1, 'd': 1}] 
    
    {'a': 1, 'c': 1, 'b': 1, 'd': 0}
    [{'a': 1, 'c': 1, 'b': 1, 'd': 0}, {'a': 1, 'c': 1, 'b': 1, 'd': 0}] 
    
    {'a': 1, 'c': 1, 'b': 0, 'd': 1}
    [{'a': 1, 'c': 1, 'b': 0, 'd': 1}, {'a': 1, 'c': 1, 'b': 0, 'd': 1}, {'a': 1, 'c': 1, 'b': 0, 'd': 1}] 
    
    {'a': 1, 'c': 1, 'b': 0, 'd': 0}
    [{'a': 1, 'c': 1, 'b': 0, 'd': 0}, {'a': 1, 'c': 1, 'b': 0, 'd': 0}, {'a': 1, 'c': 1, 'b': 0, 'd': 0}, {'a': 1, 'c': 1, 'b': 0, 'd': 0}] 
    
    {'a': 1, 'c': 0, 'b': 1, 'd': 1}
    [{'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}, {'a': 1, 'c': 0, 'b': 1, 'd': 1}] 
    
    {'a': 1, 'c': 0, 'b': 1, 'd': 0}
    [{'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}, {'a': 1, 'c': 0, 'b': 1, 'd': 0}] 
    
    {'a': 1, 'c': 0, 'b': 0, 'd': 1}
    [{'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}, {'a': 1, 'c': 0, 'b': 0, 'd': 1}] 
    
    {'a': 1, 'c': 0, 'b': 0, 'd': 0}
    [{'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}, {'a': 1, 'c': 0, 'b': 0, 'd': 0}] 
    
    {'a': 0, 'c': 1, 'b': 1, 'd': 1}
    [{'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}, {'a': 0, 'c': 1, 'b': 1, 'd': 1}] 
    
    {'a': 0, 'c': 1, 'b': 1, 'd': 0}
    [{'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}, {'a': 0, 'c': 1, 'b': 1, 'd': 0}] 
    
    {'a': 0, 'c': 1, 'b': 0, 'd': 1}
    [{'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}, {'a': 0, 'c': 1, 'b': 0, 'd': 1}] 
    
    {'a': 0, 'c': 1, 'b': 0, 'd': 0}
    [{'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}, {'a': 0, 'c': 1, 'b': 0, 'd': 0}] 
    
    {'a': 0, 'c': 0, 'b': 1, 'd': 1}
    [{'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}, {'a': 0, 'c': 0, 'b': 1, 'd': 1}] 
    
    {'a': 0, 'c': 0, 'b': 1, 'd': 0}
    [{'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}, {'a': 0, 'c': 0, 'b': 1, 'd': 0}] 
    
    {'a': 0, 'c': 0, 'b': 0, 'd': 1}
    [{'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}, {'a': 0, 'c': 0, 'b': 0, 'd': 1}] 
    
    {'a': 0, 'c': 0, 'b': 0, 'd': 0}
    [{'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}, {'a': 0, 'c': 0, 'b': 0, 'd': 0}] 
    


Answer: I think the problem is that all the items in your _all_combinations_ list are
pointed to the same _comb_dict_ , you are overwriting each element in every
call of _generate_combination_. Try to make a copy of the _comb_dict_ :

`all_combinations.append(comb_dict.copy())`

