-1

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
  • 115,751
  • 26
  • 228
  • 437
ZatX
  • 9
  • 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
  • 1
    Numbers don't have lengths; string representations of a number (in a given base) do. – chepner Mar 04 '20 at 23:15
  • The question does not make sense as asked, because none of the values in question have a length at all, so it is not possible to check whether their lengths are equal to 1. If the question is simply about how to make `any` work, please see the linked duplicate. – Karl Knechtel Aug 02 '22 at 23:48

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
  • 94,083
  • 31
  • 258
  • 268
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
  • 311
  • 2
  • 11