-1

I have a list of dictionary :

list=[{'step' : '1' , 'name' : 'A'}]

and I want to check type of the value of step key in a if condition.

I have tried :

if (x=isinstance(list[0]['step'],str)) :

But i got this error :

TypeError: isinstance() arg 2 must be a type or tuple of types

Also I tried :

list[0]['step'].__class__ == str

but got an error as well. What is the right way to do it?

Anudocs
  • 686
  • 1
  • 13
  • 54

1 Answers1

1

Don't use list as a variable name, because it's already used by python for datatype list. If you change list to mylist, the check works.

In [1]: mylist=[{'step' : '1' , 'name' : 'A'}]

In [2]: mylist 
Out[2]: [{'name': 'A', 'step': '1'}]

In [3]: if isinstance(mylist[0]["step"], str):
   ...:     print(True)
   ...:     
True
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117