Jump to content


Photo

Chp1 Nesting Issue


  • Please log in to reply
2 replies to this topic

#1 chaddow

chaddow

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 September 2014 - 09:35 AM

I'm having trouble with getting all of the list items to print. My output is missing "Graham Chapman".

Here is my movies list:

>>> print(movies)
['The Holy Grail', 1975, 'Terry Jones & Terry Gilliam', 91, ['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]]
>>> 

I then copied the code from the book and still can't get "Graham Chapman":

>>> 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)

		
The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']
>>>

After '91' I'm missing "Graham Chapman" .

What am I doing wrong here?

Thanks!

#2 chaddow

chaddow

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 September 2014 - 09:56 AM

I tried re-entering movies and the code starting back on page 21, but am not getting a similar output as the book shows. Below is the code and results.

>>> movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]
>>> print(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:
	print(each_item)

	
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:
			print(nested_item)
		else:
			print(each_item)

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

Any advice would be helpful.

Thanks!

#3 chaddow

chaddow

    New Member

  • Members
  • Pip
  • 3 posts

Posted 28 September 2014 - 07:10 AM

Figured it out.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users