0

I want to check if any length of value in array is equal to 1. I have an array

SLR = [4, 4000, 4010]

I want to check if any of these values' length: is equal to 1. In this case it's true because there's 4.

I tried to do this that way but of course it failed:

SLR = [4, 4000, 4010]

if(any(len(SLR)) == 1):
        print("True")
    else:
        print("False")
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
ZatX
  • 11
  • 5
  • 3
    It looks like SLR contains integers, so instead of `len()` maybe something like `any(n < 10 for n in SLR)` – Mark Mar 04 '20 at 22:42
  • Numbers don't have lengths; string representations of a number (in a given base) do. – chepner Mar 04 '20 at 23:15

2 Answers2

5

You can cast the int to str and check len(), i.e.:

SLR = [4, 4000, 4010]
if [x for x in SLR if len(str(x)) == 1]:
    print("yes")

Demo


Or taking advantage of short-circuiting as @kaya3 suggested:

if any(len(str(x)) == 1 for x in SLR):
    print("yes")

To use with negative numbers:

SLR = [-9, 22, 4000, 4010]
if any(-10 < n < 10 for n in SLR):
    print("yes")
Pedro Lobito
  • 85,689
  • 29
  • 230
  • 253
0

As an alternative to the other answers, you could filter the list and return a length greater than 1 for the final subset:

return len(list(filter(lambda x: len(str(x)) == 1, SLR))) >= 1
c4llmeco4ch
  • 303
  • 2
  • 11