Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:26:25+00:00 2026-06-04T09:26:25+00:00

I am new to Python and I am learning it by reading the book

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:26:26+00:00Added an answer on June 4, 2026 at 9:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to programming and am learning Python as my first language. I
i'm reading zed shaw's book learning python the hard way. Forgive me but i'm
I'm just learning about python. I'm fairly new. I have the following code that
So I'm really new to programming, I just started learning Python yesterday and I'm
I'm currently learning python from a book called 'Python for the absolute beginner (third
Learning django & python. Just set up a new site after doing the tutorial.
I am new to python and am learning how to do things the right
I'm brand new to Eclipse (learning Python). I'm really confused about the terminology within
Still new to python and django, though learning ;-) I have a view that
Fairly new to python, forgive me if this is a basic question about learning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.