I want to modify a list of tuples defined outside a function in order to change the bool value only

  function, list, python-3.x, tuples

Here is my code snippet

messages = [('food', False), ('water', False), ('sleep', False)]


def bool_changer(list_):
    for idx, word in enumerate(list_):
        # change tuple to list
        word = list(word)
        # print(word)
        if not word[1]:
            # change False to True
            word[1] = True
            # back to tuple
            word = tuple(word)
            print(word)
    return list_


print(bool_changer(messages))  # still prints the initial, no change applied
print(messages)  # still prints the initial ie [('food', False), ('water', False), ('sleep', False)]

# Desired output --> messages = [('food', True), ('water', True), ('sleep', True)]

Is there anything I need to change? coz I thought changing the inner tuples to lists would help me but in vain. Any other method is welcome btw

Source: Python-3x Questions

LEAVE A COMMENT