0

In order to check if a given list is constituted only by 0 and 1 values, I tried to set up a function returning True when the list is binary, while it returns False when not:

My code

def is_binary(y):
    for x in y:
        if x in [2,3,4,5,6,7,8,9]:
            return False
            break
        else:
            return True

Itried it on the following list:

our_list=[1,0,0,0,1,1,0,0,0,0,1,0,1,0,1,1,1]
is_binary(our_list)

Output:

True

But it doesn't work when the variable is not binary. Any help from your side will be appreciated.

5
  • 3
    You return on the first number regardless of what this number is. You'll never check anything past the first number. Move return True outside the for loop. Also, wouldn't checking if x not in [0, 1] be much more intuitive and clear? Commented Nov 23, 2022 at 14:16
  • 3
    You could use all: all(x in {0,1} for x in our_list) Commented Nov 23, 2022 at 14:18
  • It might be nicer to do something like not in [0, 1] for the edge case where you have a list with something other than ints. Commented Nov 23, 2022 at 14:18
  • 2
    You can simply return all(x in [0,1] for x in y). Commented Nov 23, 2022 at 14:18
  • set(our_list) <= {0, 1} Commented Nov 23, 2022 at 14:42

3 Answers 3

1

You can also use a list comprehension with the result of checking if each element is binary, and use all to check that all elements are True.

Edit: as @Jonh Coleman suggested, instead of creating a list first and then applying all, we can take advantage of generators, which will only generate values as requested.

We then only need to generate elements until one of them evaluates False. all will take care of that by shortcircuiting and not continuing to request elements to the generator once one of them evaluates False. That will be both more memory and time efficient.

our_list=[1,0,0,0,1,1,0,2,0,0,1,0,1,0,1,1,1]

all(i in [0,1] for i in our_list)

False
Sign up to request clarification or add additional context in comments.

3 Comments

Numpy is completely pointless there, as built-in all does exactly the same job.
Ah, I didn't know python had a builtin all, I always used the numpy one, thanks
Creating a list in memory just to run all on it is inefficient. all(i in [0,1] for i in our_list) is enough. For cases in which our_list is large but not boolean, the generator approach could short-circuit early whereas the list-based approach will process the whole list.
1
def is_binary(arr):
    for num in arr:
        if num not in [0, 1]:
            return False
    return True
    
our_list=[1,0,0,0,1,1,0,0,0,0,1,0,1,0,1,1,1]
print(is_binary(our_list))

I found several mistakes in your code.

Firstly, your code stops when the return statement is executed. So your loop will only be executed once and return the result of the first element in the list.

Secondly, there are two binary numbers, and countless numbers that aren't binary. You're just checking if the numbers are in range(2, 10). When the number is not in that range, take 11 as example, since it is not in range(2, 10), it won't execute the return False statement.

Therefore, rather to check if the number is in countless un-binary numbers, check if the number is not binary.

1 Comment

Thank you @Yeyang, your answer works but I want a short solution
1

I would turn around your logic.

def is_binary(y):
    for x in y:
        if x not in [0,1]:
             return False
    return True

The root of the problem is that you are returning the result at the first iteration round, because the return statement stops the execution of the function. This also makes your break statement redundant. See https://www.geeksforgeeks.org/python-return-statement/ for more info.

There are two use cases where your original solution works as expected and that is when the list length is one or the error is in the first item of the list y AND the "wrong" value is in the list [0,1, ...9] e.g. y=[0] or y=[1] or y=[1,0] returns True and y=[3] will return False. However, your solution fails, if y=['three'] or y=[13] or y=[0, 0, 3] because it returns True, though it should be False.

2 Comments

The break command is not the root cause. The root cause is the if-else statement.
Yes, this was actually a mistake in my answer formatting. I wanted to point out that the root of the problem is the return statement that ends the execution @GodWin. I'll edit it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.