AttributeError: 'int' object has no attribute 'isdigit' with arrays

Question:

it’s quite a simple script that I have been having a little problem with
Basically I want the script to print what’s in the array except for the integers.

array = ["hello", "hey", 1, "sup", 8]
for x in array:
  if x.isdigit():
    continue
  else:
    print(x)

I thought maybe using isnumeric.() would solve it but it seems that doesnt work too

and since im quite new it im having trobule figuring it out

Asked By: vuhkoi

||

Answers:

You want to check the type of the value. 1 is not the same as "1"; isdigit is a method defined by the str type, not the int type.

for x in array:
    if isinstance(x, str):
        print(x)
Answered By: chepner
array = ["hello", "hey", 1, "sup", 8]
for x in array:
  if isinstance(x, int):
    continue
else:
    print(x)

This will print all elements in the array that are not integers.

Answered By: Eagle123

You can check the type of a value using isinstance. Since isdigit is defined for strings only, you are getting an error when the value is an integer(other than a string).

This will print everything except integers.

array = ["hello", "hey", 1, "sup", 8, (1,2,3), {1,2,3}]

for x in array:
  if not isinstance(x, int):
    print(x)
Answered By: Abdur Rahman Asad
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.