Is there a way of toggling elements in a python list?

Viewed 582

I'm new in programming, and am finding a way (if any) to toggle the elements in a list.

I have a list where the elements can be either 1 or 0. I want to toggle them went I give a particular input.
I tried the following way:

i = input('Enter something: ')
check_var_list = ['1', '1', '0']
if i == 'Particular input':
    for element in check_var_list:
        if int(element) == 0:
            element = 1
        elif int(element) == 1:
            element = 0

But printing this is just print a copy of the original list.

How do I toggle them? Is there a function for that?
I am on Python 3.8.

5 Answers

element is a variable that holds a value from list, it doesn't "remember" where it came from, and as you've seen, overwriting it doesn't affect the list. Instead, you could iterate over the list's indexes, and update the list using the subscript ([]) operator:

for ind in range(len(check_var_list)):
    if int(check_var_list[ind]) == 0:
        check_var_list[ind] = 1
    elif int(check_var_list[ind]) == 1:
        check_var_list[ind] = 0

Try using list comprehensions instead of the above method

i = input('Enter something: ')
check_var_list = ['1', '1', '0']
if i == 'Particular input':
   check_var_list = ['1', '1', '0']
   check_var_list[:]=['0' if int(x)==1 else '1' for x in check_var_list]
print(check_var_list)

First Strings and Integers aren't intended for this purpose. Instead use True and False. Think of True as 1 and False as 0.

Second Doing element = 1 does nothing but reassign element to 1. Instead, I would eliminate the for loop entirely. Do something like this:

if i == 'abc':
    check_var_list = [not i for i in check_var_list]

You see, using the not keyword lets you get the reverse of a boolean, so not True gives False. This fixes your first problem. Your second problem is that you aren't reassigning your values. This solution uses list comprehension which loops over the original list, flipping the values, and then reassigning check_var_list to this new list.

The problem here is when you do

for k in list:

Python assigns each element of the list to the name “k”. But, “k” is just some temporary name for the element. It isn’t the element itself.

The actual list element doesn’t get modified; it just loses its name, and the updated value takes over. Try doing it via indices like

i = input('Enter something: ')
check_var_list = ['1', '1', '0']
if i == 'Particular input':
    for j in range(len(check_var_list)):
        if int(check_var_list[j]) == 0:
            check_var_list[j] = 1
        elif int(check_var_list[j]) == 1:
            check_var_list[j] = 0

There is no function for toggling values in python, but it is possible to write one. I would do something like this:

def toggle_element(element: str) -> int:
    if element == "0":
        return 1
    elif element == "1":
        return 0
    else:
        raise ValueError("Element value is not '0' or '1'.")


i = input('Enter something: ')
check_var_list = ['1', '1', '0']
if i == 'Particular input':
    check_var_list = [toggle_element(element=el) for el in check_var_list]
Related