Home > Mobile >  function is not modifying the argument?
function is not modifying the argument?

Time:12-27

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??

CodePudding user response:

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}
  • Related