0

I am new to Python and I am learning it by reading the book "Head first Python".

And my doubt is, when I want to print the nested list I am not getting the correct output as shown in the book

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

-- (Code given in book)

>>> for each_item in movies:
if isinstance(each_item, list):
    for nested_item in each_item:
        if isinstance(nested_item, list):
            for deeper_item in nested_item:
                print(deeper_item)
        else:
           print(nested_item)
else:
    print(each_item)

-- (Output in book)

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones

-- (But I got the below output only)

Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]

--

Please help me, I have tried everything i could think of. Thanks in advance.

wkl
  • 77,184
  • 16
  • 165
  • 176
iFido2020
  • 41
  • 2
  • 9
  • 2
    Even though you don't know of this as "list flattening" it is a very common question among those learning Python. Oddly, it seems only to appear in novice questions; outside of tutorials, if you didn't want a nested list, why did you make one??? – msw May 23 '12 at 05:07
  • 1
    I've not seen the book you refer to, but I've never had to use `isinstance` ever and am deeply suspicious of a book that plagues a newbie with that anti-pattern which doesn't even make sense in Java. – msw May 23 '12 at 05:12

4 Answers4

1

The code as shown will not run because of an indentation error. The first if statement should be indented. (If you got the result you have shown, you probably cut and pasted it without adjusting the indentation on this website).

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

for each_item in movies:
    if isinstance(each_item, list):
        for nested_item in each_item:
            if isinstance(nested_item, list):
                for deeper_item in nested_item:
                    print(deeper_item)
            else:
                print(nested_item)
    else:
        print(each_item)

works for me when pasted after the Python prompt. Your output is what you get when you have the final two lines indent 4 spaces less than this.

The print(nested_item) is also indented only 3 spaces where the rest (when indented) uses 4 spaces.

Anthon
  • 69,918
  • 32
  • 186
  • 246
0

The final else is mis-indented. It must be at the same level as the first if, not the first for.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

I get What you want by using your code. Please Check the indent.

for item in movies:
    if isinstance(item,list):
        for nested_item in item:
            if isinstance(nested_item,list):
                for deep_item in nitem:
                    print deep_item
            else:
                print nested_item
    else:
        print item
hakunami
  • 2,351
  • 4
  • 31
  • 50
0

Worked fine for me :)

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

for each_item in movies:
    if isinstance(each_item, list):
        for nested_item in each_item:
            if isinstance(nested_item, list):
                for deeper_item in nested_item:
                    print(deeper_item)
            else:
               print(nested_item)
    else:
        print(each_item)

I can't see any reason why it would produce that kind of output for you. Did you type it directly into the interpreter? Maybe the initial list was messed up somehow. Try coping this code into IDLE and see if anything weird happens.

machow
  • 1,034
  • 1
  • 10
  • 16