Python 2.6:Trying to run an if statement if variable is a list

Question:

I’m new to python and trying to complete an exercise where I print every variable from a list including any nested lists.

My issue is I can’t get the nested lists to be recognised as lists by an if statement.

When I run type(i) it returns that it is a list however when I run if type(i) is list or if type(i) == list it fails to execute.

When I tried using if isinstance(type(i), list) I get a TypeError:isinstance() arg 2 must be a class, type, or tuple of classes and types.

When I tried isinstance(type(i),collections.Sequence) the nested list isn’t recognised as a list either.

If anyone has any advice it’d be appreciated. I’m using Python 2.6 as I’m following the MIT course.

Thanks

# -*- coding: cp1252 -*-
import collections

listval= ["war",1,["brie","rocky","roq le coq"],[1,2,3]]

def printlist2(lists):
    for i in lists:
        print("Variable value: ", type(i))
        print ("Is variable a list: ",isinstance(type(i),collections.Sequence))
        #print (isinstance(type(i),list))
        if isinstance(type(i),collections.Sequence):
            print ("This is a list")
            printlist2(i)
        elif type(i) == list:
            print ("This is a list")
        elif type(i) is int:
            #print ("String length is equal to ",len(str(i)))
            print ("i is equal to integer ",i)
        else:
            #print ("String length is equal to ",len(i))
            print ("i is equal to string ",i) 

printlist2(listval)
Asked By: Neckr00

||

Answers:

There are a number of ways of testing for a list. Trying to use all of them at once is really confusing and unnecessary, especially for asking a question in a forum like this. I would recommend using isinstance. You just want to test against the object itself rather than taking the type() of it with isinstance

If you use that test or one of the alternate methods, the structure of your code works fine and you get exactly 2 lists pointed out during your run. Your input data isn’t demonstrating the recursion very well, but if you add more levels of embedded lists, the code will handle it. Here’s a much simplified version of your code showing that using isinstance does work:

# -*- coding: cp1252 -*-

listval= ["war",1,["brie","rocky","roq le coq"],[1,2,3]]

def printlist2(lists):
    for i in lists:
        if isinstance(i, list):
            print ("This is a list: " + str(i))
            printlist2(i)
        else:
            print ("This is not a list: " + str(i))

printlist2(listval)

Result:

This is not a list: war
This is not a list: 1
This is a list: ['brie', 'rocky', 'roq le coq']
This is not a list: brie
This is not a list: rocky
This is not a list: roq le coq
This is a list: [1, 2, 3]
This is not a list: 1
This is not a list: 2
This is not a list: 3

type(i) == list1 and type(i) is list would work too. Just pick one. This code will work with both Python 2 and Python 3. I agree with @Wombatz – use a recent version of Python 3.

Answered By: CryptoFool
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.