0

I need to check if a value is a list before carrying out the rest of the code. The for loop should only be run if my_list is a list.

    sum=0
    if not list:
        print("The input is not a list or is empty.")
        exit()

    for item in (list):
        sum+=1
    print("The length of the list is ",sum)

my_list = "Hello";
list_length(my_list)

The code works as expected when my_list is an actual list, but if I set it equal to a string, "Hello" at the moment in my code, the output is 5, I want it to not have an output.

Timothy C.
  • 45
  • 6

1 Answers1

0

you can use

if isinstance([], list):
    print("this is list")

to check. and if you want to check is subclass, you can use

if issubclass(sub_class, your_class):
    print(f"this is subclass of {your_class}")
DustyPosa
  • 463
  • 2
  • 8