Python ProgrammingPython Programming

Process a list that contains another list (or lists)

Develop a program that process a list that contains another list (or lists) using the for loop. Also use format function to display output in formatted manner.
##
#Python's program process a list that contains another list (or lists) using the for loop.
##

#Define the 4 level list
catalog = ['Men','Kids','Women',['T-shirts','Tops','Dresses','Kurta',['Red','Blue','Green','Yellow',['Cotton','Silk']]]]

for each_item in catalog:
	if isinstance(each_item, list):	#Check inner level exist
		for nested_item in each_item:
			if isinstance(nested_item, list):
				for deeper_item in nested_item:
					if isinstance(deeper_item, list):
						for deepest_item in deeper_item:
							#Display the forth level result
							print('{:^40}'.format(deepest_item))
					else:
						#Display the third level result
						print('{:^30}'.format(deeper_item))
			else:
				#Display the second level result
				print('{:^20}'.format(nested_item))
	else:
		#Display the first level result
		print('{:^10}'.format(each_item))

Sample output of above program.
C:\Python\programs>python program.py
  Men
  Kids
  Women
    T-shirts
    Tops
    Dresses
    Kurta
        Red
        Blue
        Green
        Yellow
            Cotton
            Silk

C:\Python\programs>