Python – Check if a given object is list or not



Python – Check if a given object is list or not

Given an object, the task is to check whether the object is list or not.

Method #1: Using isinstance

# Python code to demonstrate
# check whether the object 
# is a list or not
 
# initialisation list
ini_list1 = [1, 2, 3, 4, 5]
ini_list2 = '12345'
 
# code to check whether
# object is a list or not
if isinstance(ini_list1, list):
  print("your object is a list !")
else:
    print("your object is not a list")
   
if isinstance(ini_list2, list):
    print("your object is a list")
else:
    print("your object is not a list")



Output:

your object is a list !
your object is not a list

Method #2: Using type(x)

# Python code to demonstrate
# check whether object 
# is a list or not
 
# initialisation list
ini_list1 = [1, 2, 3, 4, 5]
ini_list2 = (12, 22, 33)
 
# code to check whether
# object is a list or not
if type(ini_list1) is list:
    print("your object is a list")
else:
    print("your object is not a list")
 
if type(ini_list2) is list:
    print("your object is a list")
else:
    print("your object is not a list")

Output:

your object is a list
your object is not a list

 

Last Updated on October 9, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs