peachy
peachy

Reputation: 21

function is not modifying the argument?

Alright, this function's supposed purpose is to simply modify the object "matrix" accordingly with the dictionary comprehension. However, I don't want it to return anything. I just need the modifications to stick. Is that possible??

Upvotes: 1

Views: 51

Answers (1)

CauaSCP
CauaSCP

Reputation: 16

I could resolve it by doing this:

def matrix_null(matrix: dict, null: float):
    to_pop = []
    for key, value in matrix.items():
        if value == null:
            to_pop.append(key)
    
    for key in to_pop:
        matrix.pop(key)
    
    matrix['null'] = null

# TEST:

matrix = {'abc': 123, '1': 1.0, '2': 1.0}

matrix_null(matrix, 1.0)

print(matrix)

>>> {'abc': 123, 'null': 1.0}

Upvotes: 0

Related Questions