Skip to content
Related Articles

Related Articles

Improve Article
Python | Check if a given object is list or not
  • Last Updated : 19 Mar, 2019

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :