Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type of a list is not recognized Python

I’m trying to write a program that can recognize a list but the program doesn’t work

co = {"value1": "string", "value2": [{"value": "list"}]}
print(type(co["value2"]))
if type(co["value2"]) == list:
  print("list!")
  # This is the desired result
else:
  print("other!")

The console only shows the following:

<class 'list'>
other!

The console should show the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<class 'list'>
list!

I’ve already tried to fix the error by just setting a list as "co" but the result is the same!
Maybe I’m doing very badly and have overlooked a very small thing, but I haven’t found anything
The list should recognize it so that it is processed differently than a set type
Maybe someone can help me there or has ideas to help solve the problem.

>Solution :

You code works fine, I think you might use list as a variable name above somewhere in your code and you have overridden its main functionality.

although,
it is better to use isinstance() for type checking.

so you can change your if condition like this:

if isinstance(co["value2"], list):
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading