Check list type python

Python programming language provides different built-in types like an object, list, dictionary, integer, string, etc. It can be very handy to check types and compare different variable types. There are two popular methods called type[] and isinstance[]. These methods can be used to check specified object and variable types and make decisions according to the result.

Check Type with type[] Method

The type method is one of the most popular methods to check the type of the specified object or variable or value. The object, variable, or value is provided to the type[] object as the parameter. The syntax of the type[] method is like below. The type[] method is provided as built-in and there is no need to install or import any module.

type[OBJECT] type[VARIABLE] type[VALUE]
  • OBJECT can be any object which type is returned.
  • VARIABLE can be any variable which type is returned.
  • VALUE can be any value which type is returned.

In the following example we check different objects, variables, and values type with the type[] method.

a = 1 b = "String" c = 4.5 d = [1,2,3] e = {"1":"ismail","2":"ahmet"} #Check type with Object and Variables type[a] # type[b] # type[c] # type[d] # type[e] # #Check type with Values type[1] # type["String"] # type[4.5] # type[[1,2,3]] # type[{"1":"ismail","2":"ahmet"}] #

Check Type with isinstance[] Method

The isinstance[] method provides more advanced usage than the type[] method. The isinstance[] method accepts 2 parameters. The first parameter is an object, variable, or value that is checked against the second parameter which is a type definition like str, integer, float, dict, list, etc. The isinstance[] method syntax is like below.

isinstance[OBJECT,TYPE] isinstance[VARIABLE,TYPE] isinstance[VALUE,TYPE]
  • OBJECT type checked against the TYPE.
  • VARIABLE type checked against TYPE.
  • VALUE type checked against TYPE.
  • TYPE can be str, integer, float, dict, list etc.

Check String Type

a = "ismail" isinstance[a,str] #True isinstance["ismail",str] #True

Check Integer Type

a = 2 isinstance[a,integer] #True isinstance[3,str] #True

Check Float Type

a = 1.5 isinstance[a,float] #True isinstance[1.5,str] #True

Check Tuple Type

a = [1,2,3] isinstance[a,tuple] #True isinstance[[1,2,3],tuple] #True

Check List Type

a = [1,2,3] isinstance[a,list] #True isinstance[[1,2,3],list] #True

Check Dict Type

a = {"1":"ismail","2":"ahmet"} isinstance[a,dict] #True isinstance[{"1":"ismail","2":"ahmet"},dict] #True

Check Multiple Types with isinstance[] Method

The isinstance[] method can be used to check multiple types for a single object, variable, or value. Multiple types are provided as a tuple and provided as the second parameter to the isinstance[] method. The syntax of checking multiple types with instance[] method is like below.

isinstance[OBJECT,TYPES] isinstance[VARIABLE,TYPES] isinstance[VALUE,TYPES]
  • OBJECT checked against the multiple TYPES.
  • VARIABLE checked against the multiple TYPES.
  • VALUE checked against the multiple TYPES.
  • TYPES is a tuple which contains single or more types to check aginst provided object, variable or value. As an example TYPES can be [str, integer,list].

In the following example, we check provided object, variable, and value against multiple types like integer, list, and str.

a = 1.5 b = "ismail" c = [1,2,3] isinstance[a,[int,list,str]] #False isinstance[b,[int,list,str]] #True isinstance[c,[int,list,str]] #True

In Python, to get the type of an object or check whether it is a specific type, use the built-in functions type[] and isinstance[].

This article describes the following contents.

  • Get the type of an object: type[]
  • Check the type of an object: type[], isinstance[]
    • With type[]
    • With isinstance[]
    • The difference between type[] and isinstance[]

Get the type of an object: type[]

type[] is the function that returns the type of an object passed to argument. You can use this to find out the type of a variable like typeof in other programming languages.

print[type['string']] # print[type[100]] # print[type[[0, 1, 2]]] #

The return value of type[] is type [type object] such as str or int.

print[type[type['string']]] # print[type[str]] #

Check the type of an object: type[], isinstance[]

Use type[] or isinstance[] to check whether an object is of a specific type.

With type[]

By comparing the return value of type[] with any type, you can check whether the object is of that type.

print[type['string'] is str] # True print[type['string'] is int] # False

def is_str[v]: return type[v] is str print[is_str['string']] # True print[is_str[100]] # False print[is_str[[0, 1, 2]]] # False

If you want to check if it is one of several types, use in and multiple types of tuples.

  • in operator in Python [for list, string, dictionary, etc.]

def is_str_or_int[v]: return type[v] in [str, int] print[is_str_or_int['string']] # True print[is_str_or_int[100]] # True print[is_str_or_int[[0, 1, 2]]] # False

It is also possible to define functions that change the processing depending on the argument type.

def type_condition[v]: if type[v] is str: print['type is str'] elif type[v] is int: print['type is int'] else: print['type is not str or int'] type_condition['string'] # type is str type_condition[100] # type is int type_condition[[0, 1, 2]] # type is not str or int

With isinstance[]

isinstance[object, type] returns True if the first argument object is an instance of the second argument type, or an instance of a subclass.

You can use a tuple as the second argument. Returns True if it is an instance of any type.

print[isinstance['string', str]] # True print[isinstance[100, str]] # False print[isinstance[100, [int, str]]] # True

Functions similar to the above examples using type[] can be written as follows:

def is_str[v]: return isinstance[v, str] print[is_str['string']] # True print[is_str[100]] # False print[is_str[[0, 1, 2]]] # False

def is_str_or_int[v]: return isinstance[v, [int, str]] print[is_str_or_int['string']] # True print[is_str_or_int[100]] # True print[is_str_or_int[[0, 1, 2]]] # False

def type_condition[v]: if isinstance[v, str]: print['type is str'] elif isinstance[v, int]: print['type is int'] else: print['type is not str or int'] type_condition['string'] # type is str type_condition[100] # type is int type_condition[[0, 1, 2]] # type is not str or int

The difference between type[] and isinstance[]

The difference between type[] and isinstance[] is that isinstance[] returns True even for instances of subclasses that inherit the class specified in the second argument.

For example, define the following superclass [base class] and subclass [derived class].

class Base: pass class Derive[Base]: pass base = Base[] print[type[base]] # derive = Derive[] print[type[derive]] #

type[] returns True only when the types match, but isinstance[] returns True also for the superclass.

print[type[derive] is Derive] # True print[type[derive] is Base] # False print[isinstance[derive, Derive]] # True print[isinstance[derive, Base]] # True

For example, the boolean type bool [True, False] is a subclass of int. isinstance[] returns True for both int and bool for an object of bool.

print[type[True]] # print[type[True] is bool] # True print[type[True] is int] # False print[isinstance[True, bool]] # True print[isinstance[True, int]] # True

Use type[] if you want to determine the exact type, and isinstance[] if you want to determine with considering inheritance.

A built-in function issubclass[] is also provided to check whether a class is a subclass of another class.

  • Built-in Functions - issubclass[] — Python 3.9.7 documentation

print[issubclass[bool, int]] # True print[issubclass[bool, float]] # False

Video liên quan

Chủ Đề