Issue
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:
<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):
Answered By - Mehrdad Pedramfar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.