Printing the values of a list in a list


#1

Note: This problem is not specific to any one lesson, but just an issue I encountered while doing some random coding.
My goal was to print the individual values of a list within another list, without using the list[index], thus the program should figure out on its own which elements within the list are its own list.

My code:

hi = [17,8,[4,5,1]]

for variable in hi:
  print variable
  if isinstance (variable, list):
    print hi[hi.index(variable)]
  else:
    print "False"

This returns an error:
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
This Error really surprised me, as I thought list is a datatype (also suggested to be true here: https://docs.python.org/2/library/datatypes.html)
See below another very similar piece of code which also didn’t function due to the same Error, which I found online (can’t find the exact source anymore):

isinstance([], list)
>>> True

At least, that’s what the most voted commentor on StackOverflow said it would do. All it gives me is the same Error.

What is causing this issue? what would be a solution to it?

I understand that the structure of the lists within list is not very elegant and probably confusing in any practical application, thus that it should be avoided.

Edit: I also understand that even with this error fixed my code would not be fully functional yet.


#2

i ran your code here:

https://repl.it/@stetim94/CheerfulExtrasmallBluefish

no error?


#3

Well that’s strange, I was trying to work it out on the CodeCademy editor for way too long and it gave me that error every single time (for both of the instances), now however it’s working as intended. My apologies for bothering you.

I have heard the CodeCademy editor can be buggy at times, would you recommend to try repl.it whenever something that seems like it should be working does not?


#4

i wouldn’t use the codecademy editor for any personal projects, i would set up your own development environment on your PC


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.